我有一个列表视图,显示来自firebase的数据。列表视图完美地工作。但是现在我想在我的Recycler适配器中实现一个onclick监听器,然后在新活动中显示更多细节。 我到处寻找解决方案,但无法弄明白。我知道我应该使用firebase UI。
这是使用recycleler adaper显示从firebase到listview的数据的片段:
public class CategoryFragment extends Fragment {
private static final String TAG = "CategoryFragment";
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
// private Adapter adapter;
private List<ListItem> listItems;
private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference myRef;
private AdapterView.OnItemSelectedListener listener;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView =inflater.inflate(R.layout.recycler_view,container,false);
listItems = new ArrayList<>();
adapter = new CategoryRecycler(listItems,getContext());
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
mFirebaseDatabase = FirebaseDatabase.getInstance();
getFirebaseData();
progressBar = rootView.findViewById(R.id.progressBar);
recyclerView = rootView.findViewById(R.id.my_recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setItemAnimator(new DefaultItemAnimator());
int tilePadding = getResources().getDimensionPixelSize(R.dimen.tile_padding);
recyclerView.setPadding(tilePadding, tilePadding, tilePadding, tilePadding);
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
return recyclerView;
//return rootView
}
private void getFirebaseData() {
myRef = mFirebaseDatabase.getReference("Users");
myRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
ListItem data = dataSnapshot.getValue(ListItem.class);
listItems.add(data);
recyclerView.setAdapter(adapter);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
并且我试图在回收器适配器中实现onclick:
public ViewHolder(LayoutInflater inflater, ViewGroup parent) {
super(inflater.inflate(R.layout.category_item_tile, parent, false));
username = itemView.findViewById(R.id.list_title);
imageUrl = itemView.findViewById(R.id.list_avatar);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Context context = v.getContext();
Intent pDetail = new Intent(context, ProfileDetail.class);
pDetail.putExtra(ProfileDetail.EXTRA_POSITION, getAdapterPosition());
context.startActivity(pDetail);
}
});
}
这是我的细节:
public class ProfileDetail extends AppCompatActivity {
private static final String TAG = "EXTRA_POSITION";
public static final String EXTRA_POSITION = "profile_detail";
private List<ListItem> listItems;
private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference myRef;
private TextView tvName, tvSummary, tvStatus, tvDetail;
private ImageView ivImage;
private ImageView ivImageCard;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.categorydetails);
listItems = new ArrayList<>();
ivImage = findViewById(R.id.userAvatar);
ivImageCard = findViewById(R.id.header_img);
tvName = findViewById(R.id.userProfileName);
tvSummary = findViewById(R.id.ProfileSummary);
tvStatus = findViewById(R.id.userStatus);
tvDetail = findViewById(R.id.ProfileDetail);
}
}
这是适配器的完整代码:
public class CategoryRecycler extends RecyclerView.Adapter<CategoryRecycler.ViewHolder> {
private List<ListItem> listItems;
private Context context;
public CategoryRecycler(List<ListItem>listItems,Context context) {
this.listItems = listItems;
this.context =context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()), parent);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
ListItem listItem = listItems.get(position);
holder.username.setText(listItem.getUsername());
//holder.summary.setText(listItem.getSummary());
Picasso.with(context).load(listItem.getImageurl()).into(holder.imageUrl);
}
@Override
public int getItemCount() {
return listItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView username;
public TextView summary;
public ImageView imageUrl;
public ViewHolder(LayoutInflater inflater, ViewGroup parent) {
super(inflater.inflate(R.layout.category_item_tile, parent, false));
username = itemView.findViewById(R.id.list_title);
imageUrl = itemView.findViewById(R.id.list_avatar);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Context context = v.getContext();
Intent pDetail = new Intent(context, ProfileDetail.class);
pDetail.putExtra(ProfileDetail.EXTRA_POSITION, getAdapterPosition());
context.startActivity(pDetail);
}
});
}
}
}
onBindViewHolder持有人电话:
holder.username.setText(listItem.getUsername());
Picasso.with(context).load(listItem.getImageurl()).into(holder.imageUrl);
我的毕加索图像管理需要上下文。
//声明上下文
private Context context;
//使用上下文
this.context =context;
但是如果你在没有使用图像上下文的情况下知道任何其他方式,我会很感激这些建议。
答案 0 :(得分:2)
在适配器中实现onClick操作的最佳方法是在适配器中创建一个接口,并通过使其实现此类接口将其链接到您的片段,然后在片段中启动新活动。我已经重构了你的适配器:
public class SpecialAdapter extends RecyclerView.Adapter<SpecialAdapter.SpecialViewHolder> {
List<ListItem> items;
OnItemClickListener callback;
public SpecialAdapter(List<ListItem> items, OnItemClickListener callback) {
this.items = items;
this.callback = callback;
}
@NonNull
@Override
public SpecialViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.category_item_tile, parent, false);
return new SpecialViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull SpecialViewHolder holder, int position) {
ListItem listItem = items.get(position);
holder.username.setText(listItem.getUsername());
holder.imageUrl.setVisibility(View.VISIBLE);
}
@Override
public int getItemCount() {
return items.size();
}
class SpecialViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView username;
public ImageView imageUrl;
public SpecialViewHolder(View itemView) {
super(itemView);
username = itemView.findViewById(R.id.list_title);
imageUrl = itemView.findViewById(R.id.list_avatar);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
callback.onItemClick(getAdapterPosition());
}
}
public interface OnItemClickListener {
void onItemClick(int pos);
}
}
我重构了你的片段:
public class CategoryFragment extends Fragment implements SpecialAdapter.OnItemClickListener {
private static final String TAG = "CategoryFragment";
private RecyclerView recyclerView;
private SpecialAdapter adapter;
// private Adapter adapter;
private List<ListItem> listItems;
private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference myRef;
private AdapterView.OnItemSelectedListener listener;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView =inflater.inflate(R.layout.recycler_view,container,false);
listItems = new ArrayList<>();
adapter = new SpecialAdapter(listItems, this);
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
mFirebaseDatabase = FirebaseDatabase.getInstance();
getFirebaseData();
progressBar = rootView.findViewById(R.id.progressBar);
recyclerView = rootView.findViewById(R.id.my_recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setItemAnimator(new DefaultItemAnimator());
int tilePadding = getResources().getDimensionPixelSize(R.dimen.tile_padding);
recyclerView.setPadding(tilePadding, tilePadding, tilePadding, tilePadding);
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
recyclerView.setAdapter(adapter);
return recyclerView;
//return rootView
}
private void getFirebaseData() {
myRef = mFirebaseDatabase.getReference("Users");
myRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
ListItem data = dataSnapshot.getValue(ListItem.class);
listItems.add(data);
adapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
public void onItemClick(int pos) {
Intent pDetail = new Intent(this.getContext(), ProfileDetail.class);
ListItem item = listItems.get(pos);
pDetail.putExtra(ProfileDetail.EXTRA_POSITION, item.getUserid());
startActivity(pDetail);
}
}
我认为你的ListItem看起来像这样:
public class ListItem {
private String details;
private String imageurl;
private String status;
private String summary;
private String userid;
private String username;
public String getDetails() {
return details;
}
public void setDetails(String details) {
this.details = details;
}
public String getImageurl() {
return imageurl;
}
public void setImageurl(String imageurl) {
this.imageurl = imageurl;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
最后在您的详细活动中,您只需获取此ID并阅读数据库:
public class ProfileDetail extends AppCompatActivity {
private static final String TAG = "EXTRA_POSITION";
public static final String EXTRA_POSITION = "profile_detail";
private List<ListItem> listItems;
private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference myRef;
private TextView tvName, tvSummary, tvStatus, tvDetail;
private ImageView ivImage;
private ImageView ivImageCard;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.categorydetails);
listItems = new ArrayList<>();
ivImage = findViewById(R.id.userAvatar);
ivImageCard = findViewById(R.id.header_img);
tvName = findViewById(R.id.userProfileName);
tvSummary = findViewById(R.id.ProfileSummary);
tvStatus = findViewById(R.id.userStatus);
tvDetail = findViewById(R.id.ProfileDetail);
String userid = getIntent().getExtras().getString(ProfileDetail.EXTRA_POSITION);
mFirebaseDatabase = FirebaseDatabase.getInstance();
myRef = mFirebaseDatabase.getReference("Users");
myRef.child(userid).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
ListItem item = (ListItem) dataSnapshot.getValue(ListItem.class);
if(item!=null){
tvName.setText(item.getUsername());
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
}
}
如果您不想在详细信息活动中发出请求,可以将ListItem(parcelable)序列化,并将其作为额外的意图直接放置。看这里。 Passing data through intent using Serializable