我是Android的新手,所以也许我只是缺乏理解事情的运作方式。
所以我现在拥有的是五个元素的RecyclerView。每个都包含不同形状,不同水果名称和不同颜色条的图形。
我所寻求的是创建片段,在该图中显示一些背景颜色,图形和水果名称,具体取决于Recyclerview元素用户点击的内容。我怎么能这样做?
MyAdapter类如下所示:
public class MyAdapter extends RecyclerView.Adapter <MyAdapter.MyViewHolder> {
Context context;
ArrayList<Information> data;
public MyAdapter(Context context, ArrayList<Information> data) {
this.context = context;
this.data = data;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_row,parent,false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
holder.textView.setText(data.get(position).title);
holder.relativeLayout.setBackground(context.getResources().getDrawable(data.get(position).shape));
holder.textView2.setBackground(context.getResources().getDrawable(data.get(position).color));
}
@Override
public int getItemCount() {
return data.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView textView,textView2;
RelativeLayout relativeLayout;
public MyViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.txt_view);
relativeLayout = (RelativeLayout) itemView.findViewById(R.id.img_view);
textView2 = (TextView) itemView.findViewById(R.id.textView);
}
}
}
MyActivity.java
public class MyActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
MyAdapter adapter = new MyAdapter(this, Data.getData(this));
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new GridLayoutManager(this,1));
}
}
Fragment_layout.java
public class FragmentLayout extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_layout, container, false);
}
}
activity_my.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</android.support.design.widget.CoordinatorLayout>
和 fragment_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
>
<RelativeLayout
android:id="@+id/fragment_img"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:rotation="180"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
<TextView
android:id="@+id/fragment_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:rotation="180"
android:textSize="20sp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</RelativeLayout>
答案 0 :(得分:0)
您需要在ViewHolder中为itemView创建一个onClickListener。然后,您可以使用适配器位置获取所需的详细信息,并将它们传递给您希望显示的新片段。如下例所示。
`
class MyViewHolder extends RecyclerView.ViewHolder implements` View.OnClickListener {
TextView textView,textView2;
RelativeLayout relativeLayout;
public MyViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.txt_view);
relativeLayout = (RelativeLayout) itemView.findViewById(R.id.img_view);
textView2 = (TextView) itemView.findViewById(R.id.textView);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
FruitDetailFragment detailFragment = FruitDetailFragment.newInstance(fruits.get(getAdapterPosition()).getTitle(),
fruits.get(getAdapterPosition()).getcolor());
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.my_container, detailFragment,
getString(R.string.fruit_detail_fragment_tag)).addToBackStack("FRUIT_DETAILS").commit();
}
}