我使用幾個片段制作了一個選項卡菜單。我將在每個片段中放置一個卡片視圖。我嘗試過谷歌搜索,但我所要做的就是將卡片視圖放入活動中。我想在片段中放置一個卡片視圖,然后我想在單擊卡片時實現預定義集活動的啟動。我能怎么做?我參考了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); }
1 回答

慕村225694
TA貢獻1880條經驗 獲得超4個贊
因為錯誤告訴您"error: incompatible types: CommunityFragment cannot be converted to Fragment"
您的 CommunityFragment 擴展了AppCompatActivity
并且它不是片段。
添加回答
舉報
0/150
提交
取消