Recyclerview嵌套recyclerview的深坑,对于一个新手来说怎么也上不去,特来邀请键盘中的高手高高手,拉拉我;
第一个Recyclerview布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/area_LinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/drawable_black_one"
android:orientation="horizontal"
android:layout_margin="3dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_gravity="center"
android:text=""
android:textColor="@drawable/drawable_white_pure"
android:lines="1"
android:layout_weight="1"/>
<TextView
android:layout_width="1dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_margin="5dp"
android:background="@color/whiteness"
/>
<TextView
android:id="@+id/peers_location_TextView"
android:layout_width="70dp"
android:layout_height="40dp"
android:layout_marginLeft="5sp"
android:layout_marginRight="10dp"
android:ellipsize="end"
android:gravity="center"
android:text=""
android:textColor="@color/whiteness"
android:textSize="14sp"
android:lines="1"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="0dp"
android:layout_marginEnd="10dp"
android:scaleType="fitStart"
android:src="@drawable/location" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/drawable_black_one"
android:layout_gravity="center"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_margin="3dp">
<EditText
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:background="@color/text_white"
android:hint="行业人员"
android:textSize="13dp"
android:gravity="center"
android:lines="1"
android:layout_weight="1"/>
<TextView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="搜索"
android:textSize="14dp"
android:layout_marginLeft="14dp"
android:layout_gravity="center"
android:textColor="@color/whiteness"
android:layout_margin="5dp"
android:gravity="center"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/user_info_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
在第一个Recycleview的item里面嵌套了第二个Recyclerview不知道这样对不对?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/portrait"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/user_portrait"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:src="@drawable/em_default_avatar" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_marginTop="5dp"
android:gravity="clip_horizontal">
<TextView
android:id="@+id/user_industry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/dialog_circular_whiteness"
android:ellipsize="end"
android:gravity="center"
android:text="建筑"
android:textColor="@color/colorPrimary"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:id="@+id/user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginLeft="5dp"
android:background="@drawable/dialog_circular_whiteness"
android:ellipsize="end"
android:gravity="center"
android:text="努力改变自己"
android:textColor="#323232"
android:textSize="10sp"
android:textStyle="bold" />
<TextView
android:id="@+id/name_voip"
android:layout_width="20dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:ellipsize="end"
android:gravity="center"
android:text="voip"
android:textColor="@color/text_pink"
android:textSize="7sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="35dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="努力改变自己"
android:textSize="12dp" />
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="60dp"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:id="@+id/user_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:background="@drawable/dialog_circular_whiteness"
android:text=""
android:textSize="12dp"
android:textStyle="bold" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
**<!-- 导入RecyclerView-->**
<android.support.v7.widget.RecyclerView
android:id="@+id/user_peesr_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:text="访问:10000人"
android:textSize="8dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
第一个Recyclerview的适配器:
public class Home_RecyclerView_Adapter extends RecyclerView.Adapter<Home_RecyclerView_Adapter.myViewHodler> {
private Context context;
private LinkedList<Home_list_get_set> LinkedList;
public LinkedList<User_Recruitment_get_set> mLinkedList;
public Home_list_get_set mHome_list_get_set;
//创建构造函数
public Home_RecyclerView_Adapter(Context context, LinkedList<Home_list_get_set> industrytitlegetsetList) {
//将传递过来的数据,赋值给本地变量
this.context = context;//上下文
this.LinkedList = industrytitlegetsetList;//实体类数据ArrayList
}
/**
* 创建viewhodler,相当于listview中getview中的创建view和viewhodler
*
* @param parent
* @param viewType
* @return
*/
@Override
public myViewHodler onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView user_peesr_info;//自定义recyclerveiw的适配器
//创建自定义布局
// View itemView = View.inflate(context, R.layout.peers_list_item, null);//用这个布局item宽高无效
// myViewHodler itemView = new myViewHodler(LayoutInflater.from(context).inflate(R.layout.peers_list_item, parent, false));
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.peers_list_item, parent, false);
user_peesr_info = view.findViewById (R.id.user_peesr_info);
//给嵌套的RecyclerView设置适配器
User_Recruitment_Adapter user_recruitment_adapter = new User_Recruitment_Adapter (context,mLinkedList);
user_peesr_info.setAdapter (user_recruitment_adapter);
user_peesr_info.setLayoutManager (new LinearLayoutManager (context, LinearLayoutManager.HORIZONTAL, false));
//给嵌套的RecyclerView设置item的分割线
user_peesr_info.addItemDecoration (new DividerItemDecoration (context, DividerItemDecoration.HORIZONTAL));
return new myViewHodler(view);
// return itemView;
}
/**
* 绑定数据,数据与view绑定
*
* @param holder
* @param position
*/
@SuppressLint("ClickableViewAccessibility")
@Override
public void onBindViewHolder(myViewHodler holder, int position) {
//根据点击位置绑定数据
mHome_list_get_set = LinkedList.get(position);
// holder.mItemGoodsImg;
holder.user_name.setText(mHome_list_get_set.getUser_name());
holder.user_industry.setText(mHome_list_get_set.getUser_work());
// holder.user_portrait.setImageDrawable (data.getHead_portait ());//改为下面Glide获取图上
Glide.with(context)
.load(mHome_list_get_set.getHead_portait ())//图片信息
.apply(RequestOptions.bitmapTransform(new CircleCrop ()))//设置圆形
.into(holder.user_portrait);//设置到那个部位
holder.name_voip.setText(mHome_list_get_set.getUser_Vip());
holder.user_location.setText(mHome_list_get_set.getUser_location());
}
/**
* 得到总条数
*
* @return
*/
@Override
public int getItemCount() {
// return LinkedList.size();
return LinkedList == null ? 0 : LinkedList.size();
}
//自定义viewhodler
class myViewHodler extends RecyclerView.ViewHolder {
private ImageView user_portrait;
private TextView user_name;
private TextView user_industry;
private TextView name_voip;
private TextView user_location;
public RecyclerView user_peesr_info;//自定义recyclerveiw的适配器
public myViewHodler(View itemView) {
super(itemView);
user_peesr_info = itemView.findViewById (R.id.user_peesr_info);
user_portrait = itemView.findViewById(R.id.user_portrait);
user_name = itemView.findViewById(R.id.user_name);
user_industry = itemView.findViewById(R.id.user_industry);
name_voip = itemView.findViewById(R.id.name_voip);
user_location = itemView.findViewById(R.id.user_location);
user_portrait.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//可以选择直接在本位置直接写业务处理
//Toast.makeText(context,"点击了xxx",Toast.LENGTH_SHORT).show();
//此处回传点击监听事件
if (onItemClickListener != null) {
onItemClickListener.OnItemClick(v, LinkedList.get(getLayoutPosition()));
}
}
});
}
public View getItemView() {
return itemView;
}
}
/**
* 设置item的监听事件的接口
*/
public interface OnItemClickListener {
/**
* 接口中的点击每一项的实现方法
*
* @param view 点击的item的视图
* @param data 点击的item的数据
*/
public void OnItemClick(View view, Home_list_get_set data);
}
//需要外部访问,所以需要设置set方法,方便调用
private OnItemClickListener onItemClickListener;
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
第二个Recyclerview的适配器:
public class User_Recruitment_Adapter extends RecyclerView.Adapter<User_Recruitment_Adapter.myViewHodler> {
public LinkedList<User_Recruitment_get_set> m_LinkedList;
private Context context;
public User_Recruitment_get_set mUser_Recruitment_get_set;
//创建构造函数
public User_Recruitment_Adapter(Context context, LinkedList<User_Recruitment_get_set> mLinkedList) {
//将传递过来的数据,赋值给本地变量
this.context = context;//上下文
this.m_LinkedList = mLinkedList;
}
@NonNull
@Override
public myViewHodler onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.user_peers_recruitment_item, viewGroup, false);
return new myViewHodler(view);
}
@Override
public void onBindViewHolder(@NonNull myViewHodler myViewHodler, int i) {
mUser_Recruitment_get_set = m_LinkedList.get(i);
myViewHodler.recruitment.setText (mUser_Recruitment_get_set.getRecruitment ());
}
@Override
public int getItemCount() {
return m_LinkedList == null ? 0 : m_LinkedList.size();
}
public class myViewHodler extends RecyclerView.ViewHolder {
private TextView recruitment;
public myViewHodler(@NonNull View itemView) {
super (itemView);
recruitment = itemView.findViewById(R.id.recruitment);
}
}
}