我想通过几个按钮将RecyclerView项目更改为CardView(我自己做)。我不知道如何在点击时将其转换为Cardview。
这是我的代码:
MainActivity.java
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
RVAdapter rvAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.list);
rvAdapter = new RVAdapter(getApplicationContext(), getData());
recyclerView.setAdapter(rvAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
}
private List<RVData> getData() {
List<RVData> data = new ArrayList<>();
String [] titles = {"Dummy Text1", "Dummy Text2", "Dummy Text3"};
for (int i = 0; i < titles.length; i++){
RVData current = new RVData();
current.title = titles[i];
data.add(current);
}
return data;
}
RVAdapter.java
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.RVAdapterHolder> {
private LayoutInflater inflater;
private List<RVData> data;
Context context;
public RVAdapter(Context context, List<RVData> mData) {
this.context = context;
inflater = LayoutInflater.from(context);
data = mData;
}
@Override
public RVAdapterHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.rv_row, parent, false);
RVAdapterHolder rvAdapterHolder = new RVAdapterHolder(view);
return rvAdapterHolder;
}
@Override
public void onBindViewHolder(RVAdapterHolder holder, int position) {
RVData rvData = data.get(position);
holder.textView.setText(rvData.title);
}
@Override
public int getItemCount() {
return data.size();
}
public class RVAdapterHolder extends RecyclerView.ViewHolder {
TextView textView;
public RVAdapterHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.title);
}
}
}
RVData.java
public class RVData {
public String title;
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" 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"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:fadeScrollbars="true"
android:paddingBottom="80dp"
android:scrollbars="vertical" />
</RelativeLayout>
rv_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:textColor="@color/black"
android:text="@string/dummy_title" />
</LinearLayout>
rv_card_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="match_parent"
card_view:cardCornerRadius="2dp"
card_view:cardBackgroundColor="@android:color/white"
card_view:cardElevation="2.5dp"
card_view:cardPreventCornerOverlap="true"
card_view:cardUseCompatPadding="true"/>
</LinearLayout>
最佳答案
将两种布局都放在一个布局中,然后在单击时简单地更改卡的可见性。试试这个
<?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="56dp">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
card_view:cardCornerRadius="2dp"
card_view:cardBackgroundColor="@android:color/white"
card_view:cardElevation="2.5dp"
card_view:cardPreventCornerOverlap="true"
card_view:cardUseCompatPadding="true"
android:visibility="invisible" /> <!-- this -->
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_gravity="center_vertical"
android:textColor="@color/black"
android:text="@string/dummy_title" />
</FrameLayout>
关于android - 单击将RecyclerView项目转换为CardView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30612848/