Nested RecyclerView Slide left and right to replace custom view, nested recyclerview
In the past, the left and right sliding effects were implemented using a custom scrollview or linearlayout. recyclerview can be used to achieve this function. The general requirement is either an independent left and right sliding effect, you can either slide between the left and right in the middle of a list.
The list is also easy, but it only needs to solve a small problem. I personally think it is worth mentioning that it is a high problem. Generally, people use a fixed height, however, different models are displayed in the list. If it is fixed, it is difficult to ensure the appearance, and the dynamic height can solve the problem.
First, add a recyclerview control to the layout of a list control.
<android.support.v7.widget.RecyclerView android:id="@+id/plan_recycler" android:layout_width="match_parent" android:layout_height="wrap_content"/>
Then the adapter Layout
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="match_parent" android:padding="@dimen/dimen_20dp"> <ImageView android:id="@+id/img_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:src="@drawable/bbs_plan_mofa"/> <TextView android:id="@+id/tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="@dimen/dimen_8dp" android:textSize="15sp" android:textColor="@color/color_323232"/></LinearLayout>
Next, write the adapter.
import android.content.Context;import android.support.v7.widget.RecyclerView;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;import com.xulu.loanmanager.R;import java.util.List;import butterknife.BindView;import butterknife.ButterKnife;/** * Created by LiuZhen on 2017/6/22. */public class BBSPlanAdapter extends RecyclerView.Adapter<BBSPlanAdapter.MyViewHolder> { private List<String> list; private LayoutInflater mInflater; private Context context=null; private int height; private boolean isMeasure = false; private CallBack callBack; public BBSPlanAdapter(Context context, List<String> list, CallBack callBack) { this.context=context; this.list = list; mInflater = LayoutInflater.from(context); this.callBack = callBack; } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = mInflater.inflate(R.layout.item_bbsdetail_plan, parent, false); if (!isMeasure) { view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); height = view.getMeasuredHeight(); callBack.getHeight(height); } MyViewHolder holder = new MyViewHolder(view); return holder; } public int getHeight(){ return height; } @Override public void onBindViewHolder(MyViewHolder holder, final int position) { holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { callBack.ItemClick(position); } }); } @Override public int getItemCount() { return 6; } static class MyViewHolder extends RecyclerView.ViewHolder{ @BindView(R.id.tv_content) TextView tv_content; MyViewHolder(View view){ super(view); ButterKnife.bind(this,view); } } public interface CallBack{ void getHeight(int height); void ItemClick(int position); }}
The focus is on the measure method to obtain the measured height.
Then you can use it directly.
private void initScrollList(){ final RecyclerView planRecycler = (RecyclerView) headView.findViewById(R.id.plan_recycler); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(BBSDetailActivity.this); linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); planRecycler.setLayoutManager(linearLayoutManager); List<String> list = new ArrayList<>(); BBSPlanAdapter adapter = new BBSPlanAdapter(BBSDetailActivity.this, list, new BBSPlanAdapter.CallBack() { @Override public void getHeight(int height) { ViewGroup.LayoutParams params = planRecycler.getLayoutParams(); params.height = height; planRecycler.setLayoutParams(params); } @Override public void ItemClick(int position) { Toast.makeText(BBSDetailActivity.this,""+position,Toast.LENGTH_SHORT).show(); } }); planRecycler.setAdapter(adapter); }
It is very simple. It completely replaces the custom view. The effect is as follows. If this step is not measured, the height may be inappropriate, or the text of textview cannot be seen, because it is too low, or it's too high to be beautiful.