首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将活动代码更改为分段代码?

如何将活动代码更改为分段代码?
EN

Stack Overflow用户
提问于 2019-09-22 02:25:50
回答 1查看 41关注 0票数 1

我想把活动转化为碎片。

我用片段做了一个选项卡菜单。

因为我对片段不太了解,因为我是个初学者,所以我让它变得活跃起来。

我问您,因为选项卡菜单使用片段,您不能使用我以前创建的活动。

下面是我的活动代码。如何将活动代码更改为分段代码?

代码语言: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.activity_main);

        // initialize parameter "productList"
        setInitialData();

        //Find RecyclerView from activity_main.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));
    }
}

此外,

  • 发布影响错误的代码。请查收,

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

  <!-- TODO: Update blank fragment layout -->

  <android.support.constraint.ConstraintLayout
      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="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

  </android.support.constraint.ConstraintLayout>
</FrameLayout>
EN

回答 1

Stack Overflow用户

发布于 2019-09-22 04:27:53

碎片活性

代码语言:javascript
复制
public class CommunityFragment extends Fragment {

View rootView;
List<Product> productList = new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.activity_main, container, false);
    // initialize parameter "productList"
    setInitialData();

    //Find RecyclerView from activity_main.xml
    RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.list);

    // Create LinearLayoutManager and set it to RecyclerView
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(linearLayoutManager);

    //Create MyAdapter object with the parameters and set to RecyclerView
    MyAdapter myAdapter = new MyAdapter(getActivity(), productList);
    recyclerView.setAdapter(myAdapter);


    return rootView;
}
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));
}

}

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

https://stackoverflow.com/questions/58045548

复制
相关文章

相似问题

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