我想把活动转化为碎片。
我用片段做了一个选项卡菜单。
因为我对片段不太了解,因为我是个初学者,所以我让它变得活跃起来。
我问您,因为选项卡菜单使用片段,您不能使用我以前创建的活动。
下面是我的活动代码。如何将活动代码更改为分段代码?
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>发布于 2019-09-22 04:27:53
碎片活性
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));
}}
https://stackoverflow.com/questions/58045548
复制相似问题