我有一个显示Fragment
的{{1}}(FragmentColourSets
)。我希望能够单击RecyclerView
中的某个项目,然后查看显示该项目信息的RecyclerView
。我期望DialogFragment
中有一个OnClickListener
可以处理此问题,但是现在我发现没有了,我被卡住了。我发现的解决方案涉及在适配器中添加点击侦听器,但是从那里我无法访问RecyclerView
或FragmentManager
来显示Context
。
因此,DialogFragment
布局的相关部分如下:
FragmentColourSets
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_colour_sets"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
类是(我省略了一些无关紧要的内容以使其更短):
FragmentColourSets
public class FragmentColourSets extends Fragment {
private ArrayList<ColourSet> mColourSets;
private Context mContext;
private FirebaseFirestore mDb;
private RecyclerView mRVColourSets;
private String mUid;
private FragmentManager mFragmentManager;
static public FragmentCurrentColourSets newInstance(Context context, ArrayList<ColourSet> colourSets) {
FragmentCurrentColourSets f = new FragmentColourSets();
f.setRequiredData(context, colourSets);
return f;
}
public void setRequiredData(Context context, ArrayList<ColourSet> colourSets) {
this.mContext = context;
this.mCurrentColourSets = colourSets;
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
// Inflate layout
View rootView = inflater.inflate(R.layout.fragment_colour_sets, container, false);
mRVColourSets = rootView.findViewById(R.id.rv_colour_sets);
mFragmentManager = getChildFragmentManager();
// Set up Layout Manager, and set Recycler View to use it
LinearLayoutManager mManager = new LinearLayoutManager(getActivity());
mManager.setReverseLayout(true);
mManager.setStackFromEnd(true);
mRVColourSets.setLayoutManager(mManager);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mContext = getActivity();
}
// Set colour sets to be shown
public void setColourSets(ArrayList<ColourSet> coloursets) {
mColourSets = coloursets;
fillView();
}
public void setUid(String thisUid) { mUid = thisUid; }
public void setDb(FirebaseFirestore db) { mDb = db; }
/**
* fillView
* List current Colour Sets in recycler view
* This takes the colour sets and shows them
**/
private void fillView() {
if(mRVColourSets == null) return;
if(mColourSets == null) return;
AdapterListColourSets mAdapter = new AdapterListColourSets(mColourSets);
mRVColourSets.setAdapter(mAdapter);
}
}
是:
AdapterListColourSets
因此,在某个时候,我需要为public class AdapterListColourSets extends RecyclerView.Adapter<AdapterListColourSets.ViewHolderColourSet> {
private ArrayList<ColourSet> colourSets;
public AdapterListColourSets(ArrayList<ColourSet> colourSets) {
this.colourSets=colourSets;
}
@NonNull
@Override
public ViewHolderColourSet onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View layoutColourSets = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_colour_set, null);
return new ViewHolderColourSet(layoutColourSets);
}
@Override
public void onBindViewHolder(@NonNull ViewHolderColourSet holder, int position) {
holder.colourSetName.setText(colourSets.get(position).getName());
}
@Override
public int getItemCount() {
return colourSets.size();
}
class ViewHolderColourSet extends RecyclerView.ViewHolder {
TextView colourSetName, colourSetKey;
ViewHolderColourSet(View itemView) {
super(itemView);
colourSetName = itemView.findViewById(R.id.tv_colour_set_name);
colourSetKey = itemView.findViewById(R.id.tv_colour_set_key);
}
}
}
中显示的每个项目附加一个OnClick侦听器,以便可以执行以下操作:
RecyclerView
但是,在适配器内,我无权访问private void onClick() {
DFEditColourSet dfEdit
= DFEditColourSet.newInstance(mContext);
dfEdit.show(mFragmentManager, "Edit Colour Set");
}
或Context
。我应该怎么做?
答案 0 :(得分:1)
有很多资源向您展示如何做到这一点。
看看这个blog post。
关于上下文在适配器中不可用的问题:只需将其作为构造函数参数传入即可。 这可以在this example repository中看到。
可以使用SharedViewModel
如果您无法解决,请随时提出更多问题。
编辑 我还发现this answer是一个类似的问题,它可能提供比我提到的博客文章更易于实现的解决方案。