-3
我是Android新手,所以也許我只是缺乏理解工作方式。如何創建顯示單擊RecyclerView的元素的片段?
所以我現在所擁有的是五行元素的RecyclerView。它們每個都包含不同形狀的圖形,不同的水果名稱和不同的顏色條。
我追求的是創建Fragment,根據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>