国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android嵌套RecyclerView左右滑動替代自定義view

2019-12-12 02:37:09
字體:
供稿:網(wǎng)友

以前的左右滑動效果采用自定義scrollview或者linearlayout來實(shí)現(xiàn),recyclerview可以很好的做這個(gè)功能,一般的需求就是要么一個(gè)獨(dú)立的左右滑動效果,要么在一個(gè)列表里的中間部分一個(gè)左右滑動效果

而列表里面也容易,只是需要解決一點(diǎn)小問題,個(gè)人認(rèn)為值得一提的就是高度問題,一般的人采用固定死的高度,可是在列表里面展示和機(jī)型的不同,固定死的話很難保證美觀,動態(tài)的高度才能解決問題的所在

首先在一個(gè)列表控件布局上添加一個(gè)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); }}

重點(diǎn)是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,效果如下,如果沒有測量這一步可能會出現(xiàn)高度不適合,要么是看不到textview的文字,因?yàn)樘土耍淳褪翘吡耍幻烙^。

以上所述是小編給大家介紹的Android嵌套RecyclerView左右滑動替代自定義view,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對武林網(wǎng)網(wǎng)站的支持!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 通州市| 宁明县| 沁阳市| 凭祥市| 漠河县| 托里县| 土默特左旗| 永顺县| 广宗县| 遂川县| 桦川县| 黎川县| 建湖县| 怀来县| 天峻县| 青川县| 麦盖提县| 昭觉县| 高台县| 波密县| 凯里市| 红安县| 教育| 柳河县| 闻喜县| 深圳市| 开远市| 禄劝| 阳原县| 申扎县| 宜兰市| 象山县| 晋州市| 密云县| 千阳县| 漳平市| 湄潭县| 岫岩| 溧水县| 城口县| 嘉黎县|