【发布时间】:2018-03-26 14:45:21
【问题描述】:
我有一个CardViews 的列表。每个CardView(代表一个挑战)都有一个ViewPager,以显示“解决方案”的垂直可滚动列表。
“解决方案列表”中的数据和图像来自Firebase 数据库。
我遇到的问题是您打开的第一个 CardView(当您单击它时,它会展开并显示解决方案)正确显示所有解决方案。当您单击第二个时,它会展开但没有显示任何数据,我认为它是这样的,因为我必须使用“最终”布局(final FrameLayout v0 = (FrameLayout) inflater.inflate(R.layout.solutions, null);)
以下是自定义RecyclerView适配器的重要部分的sn-p。
public class ChallengesAdapter extends
RecyclerView.Adapter<ChallengesAdapter.RecyclerItemViewHolder> {
private ArrayList<ChallengesData> myChallengesList;
int mLastPosition = 0;
//Firebase
String mUID = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference challengeSolutionsRef = rootRef.child("challenges");
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageRef = storage.getReferenceFromUrl("firebase-url");
public ChallengesAdapter(ArrayList<ChallengesData> myChallengesList) {
this.myChallengesList = myChallengesList;
}
public RecyclerItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.challenges_row, parent, false);
RecyclerItemViewHolder holder = new RecyclerItemViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(RecyclerItemViewHolder holder, final int position) {
holder.etTitleTextView.setText(myChallengesList.get(position).getTitle());
holder.etDescriptionTextView.setText(myChallengesList.get(position).getDescription());
mLastPosition =position;
}
@Override
public int getItemCount() {
return(null != myChallengesList ? myChallengesList.size():0);
}
public void notifyData(ArrayList<ChallengesData> myList) {
this.myChallengesList = myList;
notifyDataSetChanged();
}
public class RecyclerItemViewHolder extends RecyclerView.ViewHolder {
private final TextView etTitleTextView;
private final TextView etDescriptionTextView;
//private final TextView etContent;
private final ChallengesPagerAdapter myChallengesPagerAdapter;
private final ViewPager myViewPager;
private final FrameLayout myFrameLayout;
private LinearLayout mainLayout;
public RecyclerItemViewHolder(final View parent) {
super(parent);
etTitleTextView = (TextView) parent.findViewById(R.id.txtTitle);
etDescriptionTextView = (TextView) parent.findViewById(R.id.txtDescription);
myViewPager = (ViewPager) parent.findViewById(R.id.view_pager);
myChallengesPagerAdapter = new ChallengesPagerAdapter();
myViewPager.setAdapter(myChallengesPagerAdapter);
myFrameLayout = (FrameLayout) parent.findViewById(R.id.framelayout_view_pager);
mainLayout = (LinearLayout) parent.findViewById(R.id.mainLayout);
mainLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
challengeSolutionsRef = challengeSolutionsRef.child(myChallengesList.get(getAdapterPosition()).getChallengeKey()).child("solutions");
challengeSolutionsRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
final LayoutInflater inflater = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
DatabaseReference solutionsRef = rootRef.child("solutions").child(dataSnapshot.getKey());
solutionsRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
final FrameLayout v0 = (FrameLayout) inflater.inflate(R.layout.solutions, null);
TextView TVSolutionID = (TextView)v0.findViewById(R.id.txtSolutionID);
TextView TVUserID = (TextView)v0.findViewById(R.id.txtUserID);
TVSolutionID.setText(dataSnapshot.getKey());
String UserID = dataSnapshot.child("userId").getValue(String.class);
TVUserID.setText(UserID);
myChallengesPagerAdapter.notifyDataSetChanged();
//get file from cloud
storageRef.child("solutions/").child(dataSnapshot.child("images").getValue(String.class) + ".jpg").getBytes(Long.MAX_VALUE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
// Use the bytes to display the image
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
ImageView IVSolution = (ImageView)v0.findViewById(R.id.ivSolutionImage);
IVSolution.setImageBitmap(bitmap);
myChallengesPagerAdapter.notifyDataSetChanged();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
ImageView IVSolution = (ImageView)v0.findViewById(R.id.ivSolutionImage);
IVSolution.setImageResource(R.mipmap.ic_challenger_black);
myChallengesPagerAdapter.notifyDataSetChanged();
}
});
myChallengesPagerAdapter.addView(v0);
myChallengesPagerAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.d("Test","The read failed: " + databaseError.getCode());
}
});
}
....
});
....
}
});
}
}
}
这是challenge_row.xml:
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_margin="8dp"
android:id="@+id/card_view"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/txtTitle"
android:padding="12dp"
android:layout_width="match_parent"
android:textColor="@android:color/black"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/txtDescription"
android:padding="12dp"
android:layout_width="match_parent"
android:textColor="@android:color/black"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@+id/framelayout_view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:visibility="gone">
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="300dp">
</android.support.v4.view.ViewPager>
<ImageButton
android:id="@+id/arrow_back"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center_vertical|left"
android:src="@drawable/ic_arrow_back" />
<ImageButton
android:id="@+id/arrow_forward"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center_vertical|right"
android:src="@drawable/ic_arrow_forward" />
</FrameLayout>
</LinearLayout>
这是solutions.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/txtSolutionID"
android:padding="12dp"
android:layout_width="match_parent"
android:textColor="@android:color/black"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/txtUserID"
android:padding="12dp"
android:layout_width="match_parent"
android:textColor="@android:color/black"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/ivSolutionImage"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</FrameLayout>
根据 Marcos 的建议进行编辑:(仍然是相同的结果)
public class ChallengesAdapter extends RecyclerView.Adapter<ChallengesAdapter.RecyclerItemViewHolder> {
private ArrayList<ChallengesData> myChallengesList;
int mLastPosition = 0;
//Firebase
String mUID = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference challengeSolutionsRef = rootRef.child("challenges");
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageRef = storage.getReferenceFromUrl("firebase-url");
public ChallengesAdapter(ArrayList<ChallengesData> myChallengesList) {
this.myChallengesList = myChallengesList;
}
public RecyclerItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.challenges_row, parent, false);
RecyclerItemViewHolder holder = new RecyclerItemViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(final RecyclerItemViewHolder holder, final int position) {
holder.etTitleTextView.setText(myChallengesList.get(position).getTitle());
holder.etDescriptionTextView.setText(myChallengesList.get(position).getDescription());
mLastPosition =position;
holder.mainLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(holder.itemView.getContext(), "Position:" + myChallengesList.get(holder.getAdapterPosition()).getDescription(), Toast.LENGTH_SHORT).show();
//create reference to solutions inside challenges data
challengeSolutionsRef = challengeSolutionsRef.child(myChallengesList.get(holder.getAdapterPosition()).getChallengeKey()).child("solutions");
challengeSolutionsRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
DatabaseReference solutionsRef = rootRef.child("solutions").child(dataSnapshot.getKey());
solutionsRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
holder.v0 = (FrameLayout) holder.myInflater.inflate(R.layout.solutions, null);
TextView TVSolutionID = (TextView)holder.v0.findViewById(R.id.txtSolutionID);
TextView TVUserID = (TextView)holder.v0.findViewById(R.id.txtUserID);
TVSolutionID.setText(dataSnapshot.getKey());
String UserID = dataSnapshot.child("userId").getValue(String.class);
TVUserID.setText(UserID);
holder.myChallengesPagerAdapter.notifyDataSetChanged();
storageRef.child("Solutions/").child(dataSnapshot.child("picture").getValue("filename").getBytes(Long.MAX_VALUE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
// Use the bytes to display the image
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
ImageView IVSolution = (ImageView)holder.v0.findViewById(R.id.ivSolutionImage);
IVSolution.setImageBitmap(bitmap);
holder.myChallengesPagerAdapter.notifyDataSetChanged();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
//Create new item in the rowItems arraylist to add to the listview
ImageView IVSolution = (ImageView)holder.v0.findViewById(R.id.ivSolutionImage);
IVSolution.setImageResource(R.mipmap.ic_challenger_black);
holder.myChallengesPagerAdapter.notifyDataSetChanged();
}
});
holder.myChallengesPagerAdapter.addView(holder.v0);
holder.myChallengesPagerAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.d("Test","The read failed: " + databaseError.getCode());
}
});
}
...
});
});
}
@Override
public int getItemCount() {
return(null != myChallengesList ? myChallengesList.size():0);
}
public void notifyData(ArrayList<ChallengesData> myList) {
this.myChallengesList = myList;
notifyDataSetChanged();
}
public class RecyclerItemViewHolder extends RecyclerView.ViewHolder {
private TextView etTitleTextView;
private TextView etDescriptionTextView;
//private final TextView etContent;
private ChallengesPagerAdapter myChallengesPagerAdapter;
private ViewPager myViewPager;
private FrameLayout myFrameLayout;
private LinearLayout mainLayout;
private View myParent;
private LayoutInflater myInflater;
private FrameLayout v0;
public RecyclerItemViewHolder(final View parent) {
super(parent);
etTitleTextView = (TextView) parent.findViewById(R.id.txtTitle);
etDescriptionTextView = (TextView) parent.findViewById(R.id.txtDescription);
myViewPager = (ViewPager) parent.findViewById(R.id.view_pager);
myChallengesPagerAdapter = new ChallengesPagerAdapter();
myViewPager.setAdapter(myChallengesPagerAdapter);
myFrameLayout = (FrameLayout) parent.findViewById(R.id.framelayout_view_pager);
mainLayout = (LinearLayout) parent.findViewById(R.id.mainLayout);
myParent = parent;
myInflater = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
}
}
【问题讨论】:
-
将创建 ViwHolder 时的 addActionListeners 更改为 onBindViewHolder
-
我已经进行了您建议的更改,但似乎无法修复它。可以看一下帖子底部的代码吗?
标签: android firebase firebase-realtime-database firebase-storage android-cardview