【发布时间】:2016-03-23 18:04:20
【问题描述】:
我有这个带有 RecyclerView 的片段
package com.example.myapplication;
import android.annotation.TargetApi;
import android.app.FragmentManager;
import android.os.Build;
import android.os.Bundle;
import android.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import java.util.ArrayList;
import java.util.List;
public class Fragment_Username_Search extends Fragment {
private List<User> userList = new ArrayList<>();
private RecyclerView recyclerView;
private UserAdapter userAdapter;
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
private void prepareUserData() {
User user = new User(R.drawable.emilypic,"Marry Young","marbear",R.id.item_follow_button);
userList.add(user);
user = new User(R.drawable.emilypic,"Marry Young","marbear",R.id.item_follow_button);
userList.add(user);
user = new User(R.drawable.emilypic,"Sammy Lee","sambam",R.id.item_follow_button);
userList.add(user);
user = new User(R.drawable.emilypic,"Lilly Martinson","lilmartini",R.id.item_follow_button);
userList.add(user);
userAdapter.notifyDataSetChanged();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment__username__search, container, false);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
recyclerView = (RecyclerView) view.findViewById(R.id.RecyclerView);
userAdapter = new UserAdapter(userList);
prepareUserData();
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(userAdapter);
userAdapter.SetOnItemClickListener(new UserAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
FragmentManager fm = getFragmentManager();
android.app.DialogFragment dialogFragment = new DialogFragment();
dialogFragment.show(fm, "Sample Fragment");
}
});
return view;
}
}
什么时候我必须使用preparedata()?当我在之后使用这个时
userAdapter = new UserAdapter(userList);
然后我的列表中只有一项。但是在准备数据中是 4 个元素,我该如何解决?当我将 preparedata() 放在这段代码之前(初始化适配器)我有错误,因为 notifyDataSetChanged 召回空对象。 用户适配器:
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.MyViewHolder> {
private List<User> UserList;
OnItemClickListener mItemClickListener;
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder = null;
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View viewUser = inflater.inflate(R.layout.row_item_layout,parent,false);
viewHolder = new MyViewHolder(viewUser,mItemClickListener);
return (MyViewHolder) viewHolder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.UserTitle.setText(UserList.get(position).getTitle());
holder.UserDescription.setText(UserList.get(position).getDescription());
holder.UserIcon.setImageResource(R.drawable.emilypic);
}
public UserAdapter(List<User> UserList){
this.UserList = UserList;
}
@Override
public int getItemCount() {
return UserList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public ImageView UserIcon;
public TextView UserTitle;
public TextView UserDescription;
public Button UserFollowButton;
public MyViewHolder(View itemView, UserAdapter.OnItemClickListener clickListener) {
super(itemView);
clickListener = (OnItemClickListener) mItemClickListener;
UserIcon = (ImageView) itemView.findViewById(R.id.item_icon);
UserTitle = (TextView) itemView.findViewById(R.id.item_title);
UserDescription = (TextView) itemView.findViewById(R.id.item_description);
UserFollowButton = (Button) itemView.findViewById(R.id.item_follow_button);
//itemView.setOnClickListener(this);
UserFollowButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
mItemClickListener.onItemClick(v, getPosition());
}
}
public interface OnItemClickListener{
public void onItemClick(View view,int position);
}
public void SetOnItemClickListener(final OnItemClickListener mItemClickListener) {
this.mItemClickListener = mItemClickListener;
}
}
片段布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.myapplication.Fragment_Username_Search">
<LinearLayout
android:id="@+id/Search_field"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:focusable="true"
android:focusableInTouchMode="true"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_search_black_48dp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:tag="Search"
android:textIsSelectable="false"
/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:layout_below="@id/Search_field"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/RecyclerView"
>
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
项目布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:id="@+id/item_icon"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="1dp"
/>
<LinearLayout
android:layout_toRightOf="@id/item_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/item_title"
android:textSize="22dp"
android:textColor="@android:color/darker_gray"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/item_description"
android:textSize="16dp"
/>
</LinearLayout>
<Button
android:layout_width="90dp"
android:layout_height="40dp"
android:id="@+id/item_follow_button"
android:layout_marginRight="30dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:background="@drawable/follow_btn"
/>
</RelativeLayout>
【问题讨论】:
-
"那么我的列表中只有一项" - 这是否意味着
(getItemCount() == 1)? -
我在屏幕上只看到了一项
-
很抱歉投了反对票(不是我,我喜欢有足够代码来重现问题的问题)我只是将您的代码放入我的一个应用程序中。好消息:我有四个列表项。所以我认为你的问题的根源一定是我必须修补的部分:你没有发布任何布局文件。
-
没问题,非常感谢您的帮助。我会添加布局文件。
-
@Rost 你能打印吗 -- public int getItemCount() { return UserList.size(); //打印这个 }
标签: android android-fragments android-recyclerview android-sdk-tools