首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >把卡纸放在碎片里面

把卡纸放在碎片里面
EN

Stack Overflow用户
提问于 2019-09-21 10:52:11
回答 1查看 104关注 0票数 1

我用几个片段做了一个选项卡菜单。我要在每一块碎片上放一张卡纸。我试着在谷歌上搜索,但我所要做的就是在活动中放上一张卡纸。

我想在片段中放置一个硬视图,然后当我点击卡片时,我想实现预定义的set活动的启动。

我该怎么做?

代码语言:javascript
复制
<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文件)

代码语言:javascript
复制
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代码

代码语言:javascript
复制
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不能转换为片段

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-22 11:34:38

正如错误告诉您的,"error: incompatible types: CommunityFragment cannot be converted to Fragment",您的CommunityFragment扩展了AppCompatActivity,而不是片段。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58039558

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档