Как я могу изменить код активности на код фрагментации? - PullRequest
1 голос
/ 22 сентября 2019

Я хочу охватить деятельность фрагментом.

Я создал меню вкладок, используя Fragment.

Поскольку я плохо понимал Fragment, потому что был новичком, я сделал его активным.

Я спрашиваю вас, потому что в меню вкладок используется Фрагмент, вы не можете использовать действие, которое я создал ранее.

Ниже приведен мой код активности.Как я могу изменить код активности на код фрагментации?

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));
    }
}
  • Кроме того, опубликуйте код, который повлиял на ошибку.Пожалуйста, проверьте.
<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>

1 Ответ

2 голосов
/ 22 сентября 2019

Активность к фрагменту

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

}

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...