【发布时间】:2020-01-22 04:09:14
【问题描述】:
我使用几个片段制作了一个标签菜单。我将在每个片段中放置一个卡片视图。 我尝试了谷歌搜索,但我所要做的就是将卡片视图置于活动状态。
我想在 Fragment 中放置一个卡片视图,然后我想在我点击卡片时实现一个预定义的集合活动的启动。
我该怎么办?
我将https://www.codingdemos.com/android-tablayout-example-viewpager 称为标签菜单。
我附上我的片段布局(xml 文件)代码
<FrameLayout 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"
tools:context="com.aeyoung.csw.CommunityFragment">
<!-- TODO: Update blank fragment layout -->
<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"
tools:context="com.aeyoung.csw.CommunityFragment">
<android.support.v7.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
</FrameLayout>
- 我还添加了片段代码(java 文件)
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class CommunityFragment extends AppCompatActivity {
//Create parameter
List<Product> productList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_community);
// initialize parameter "productList"
setInitialData();
//Find RecyclerView from fragment_community.xml
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list);
// Create LinearLayoutManager and set it to RecyclerView
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
//Create MyAdapter object with the parameters and set to RecyclerView
MyAdapter myAdapter = new MyAdapter(this, productList);
recyclerView.setAdapter(myAdapter);
}
private void setInitialData() {
productList.add(new Product("text1", "text1", R.mipmap.ic_launcher));
productList.add(new Product("text2", "text2", R.mipmap.ic_launcher));
productList.add(new Product("text3", "text3", R.mipmap.ic_launcher));
productList.add(new Product("text4", "text4", R.mipmap.ic_launcher));
productList.add(new Product("text5", "text5", R.mipmap.ic_launcher));
productList.add(new Product("text6", "text6", R.mipmap.ic_launcher));
productList.add(new Product("text7", "text7", R.mipmap.ic_launcher));
productList.add(new Product("text8", "text8", R.mipmap.ic_launcher));
productList.add(new Product("text9", "text9", R.mipmap.ic_launcher));
productList.add(new Product("text10", "text10", R.mipmap.ic_launcher));
}
}
PageAdapter.java 代码
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
/**
* Created by abdalla on 2/18/18.
*/
public class PageAdapter extends FragmentPagerAdapter {
private int numOfTabs;
PageAdapter(FragmentManager fm, int numOfTabs) {
super(fm);
this.numOfTabs = numOfTabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new MajorFragment();
case 1:
return new FresherFragment();
case 2:
return new CommunityFragment();
case 3:
return new SettingsFragment();
default:
return null;
}
}
@Override
public int getCount() {
return numOfTabs;
}
}
并且在构建时出现以下错误。 “错误:不兼容的类型:CommunityFragment 无法转换为 Fragment”
【问题讨论】:
-
并且没有区别。用卡片膨胀布局并在其上设置 onClickListener。
-
你现在有什么问题?你可以同时做...把cardView放在活动和片段中..!
-
@MasoudDarzi 我尝试了很多次cardview,但这不起作用。我找到了一个站点,并转至this site。但也不起作用。
-
@JeongGaon 将您的代码添加到问题中
-
@SamanSalehi 我在问题中添加了代码。
标签: java android android-fragments cardview