嵌套RecyclerView左右滑動替代自訂view,嵌套recyclerview

來源:互聯網
上載者:User

嵌套RecyclerView左右滑動替代自訂view,嵌套recyclerview

以前的左右滑動效果採用自訂scrollview或者linearlayout來實現,recyclerview可以很好的做這個功能,一般的需求就是要麼一個獨立的左右滑動效果,要麼在一個列表裡的中間部分一個左右滑動效果

而列表裡面也容易,只是需要解決一點小問題,個人認為值得一提的就是高度問題,一般的人採用固定死的高度,可是在列表裡面展示和機型的不同,固定死的話很難保證美觀,動態高度才能解決問題的所在

首先在一個清單控制項布局上添加一個recyclerview控制項

<android.support.v7.widget.RecyclerView        android:id="@+id/plan_recycler"        android:layout_width="match_parent"        android:layout_height="wrap_content"/>

 

然後是adapter適配器布局

<?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>

接下來寫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);    }}

重點是measure方法,得到測量的高度

接下來就可以直接使用了

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);    }

很簡單,完全替代自訂view,效果如下,如果沒有測量這一步可能會出現高度不適合,要麼是看不到textview的文字,因為太低了,要麼就是太高了,不美觀

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.