亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2017年06月23日 08:36:15   作者:世人笑我太瘋癲  
這篇文章主要介紹了Android嵌套RecyclerView左右滑動替代自定義view,非常不錯,具有參考借鑒價值,需要的朋友可以參考下

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

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

首先在一個列表控件布局上添加一個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的文字,因為太低了,要么就是太高了,不美觀。

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

相關文章

  • React?Native之在Android上添加陰影的實現

    React?Native之在Android上添加陰影的實現

    這篇文章主要介紹了React?Native之在Android上添加陰影的實現方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Android--SQLite(增,刪,改,查)操作實例代碼

    Android--SQLite(增,刪,改,查)操作實例代碼

    Android--SQLite(增,刪,改,查)操作實例代碼,需要的朋友可以參考一下
    2013-02-02
  • 淺談Android Studio3.0更新之路(遇坑必入)

    淺談Android Studio3.0更新之路(遇坑必入)

    這篇文章主要介紹了淺談Android Studio3.0更新之路(遇坑必入),小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • Kotlin對象的懶加載方式by?lazy?與?lateinit?異同詳解

    Kotlin對象的懶加載方式by?lazy?與?lateinit?異同詳解

    這篇文章主要為大家介紹了Kotlin對象的懶加載方式by?lazy?與?lateinit?異同詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-10-10
  • Android編程學習之異步加載圖片的方法

    Android編程學習之異步加載圖片的方法

    這篇文章主要介紹了Android編程學習之異步加載圖片的方法,以實例形式較為詳細的分析了Android異步加載圖片所涉及的頁面布局及功能實現技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-10-10
  • Android仿頭條、微信大圖預覽視圖的方法詳解

    Android仿頭條、微信大圖預覽視圖的方法詳解

    大圖預覽應該對大家來說都不陌生,下面這篇文章主要給大家介紹了關于Android仿頭條、微信大圖預覽視圖的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友們下面隨著小編來一起學習學習吧。
    2018-03-03
  • PowerManagerService之自動滅屏流程解析

    PowerManagerService之自動滅屏流程解析

    這篇文章主要為大家介紹了PowerManagerService之自動滅屏流程解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-10-10
  • Android入門之Handler的使用教程詳解

    Android入門之Handler的使用教程詳解

    這篇文章主要為大家詳細介紹了Android中Handler機制的使用,文中的示例代碼講解詳細,有需要的朋友可以借鑒參考下,希望能夠對大家有所幫助,
    2022-11-11
  • 關于RxJava的一些特殊用法小結

    關于RxJava的一些特殊用法小結

    RxJava 是一個響應式編程框架,采用觀察者設計模式。下面這篇文章主要總結介紹了一些關于RxJava的特殊用法,需要的朋友可以參考借鑒,下面跟著小編一起來學習學習吧。
    2017-05-05
  • Android Studio全局搜索快捷鍵(Ctrl+Shift+F)失效問題及解決

    Android Studio全局搜索快捷鍵(Ctrl+Shift+F)失效問題及解決

    這篇文章主要介紹了Android Studio全局搜索快捷鍵(Ctrl+Shift+F)失效問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01

最新評論