如何加载/生成一个单独的cardview.xml到一个片段类



我有一个片段,我想在片段中生成一些卡片视图。首先我用java编程:

CardView cardView = new CardView(context);
cardView.setId(id);
cardView.setBackgroundColor(Color.WHITE);
cardView.setLayoutParams( new
LinearLayout.LayoutParams(MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));

这样,我可以添加尽可能多的卡片视图,我想片段。我还在卡片视图中添加了一个消耗性按钮、一个开关和一个表视图。这一切都工作,但是,我的代码真的很乱。所以我想知道,我可以有一个XML模板布局的卡片视图,并加载到Fragment类多次我想要的,并绑定数据到每个卡片视图?

我改变了策略。我只是生成嵌套的片段,而不是试图在片段中生成视图。

<<p>父片段/strong>
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import com.app.ssl.R;
import com.app.ssl.databinding.FragmentChildfragmentBinding;
public class ParentFragment extends Fragment {
private FragmentChildfragmentBinding binding;
private  View root;
public View onCreateView(
@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceStage) {
binding = FragmentChildfragmentBinding.inflate(inflater, container, false);
root = binding.getRoot();
FragmentManager fragMan = getChildFragmentManager();
FragmentTransaction fragTrans = fragMan.beginTransaction();
Fragment fragA = new NestedFragment("A");
fragTrans.add(R.id.nested_fragment_container, fragA);
fragTrans.addToBackStack("A");
Fragment fragB = new NestedFragment("B");
fragTrans.add(R.id.nested_fragment_container, fragB);
fragTrans.addToBackStack("B");
Fragment fragC = new NestedFragment("C");
fragTrans.add(R.id.nested_fragment_container, fragC);
fragTrans.addToBackStack("C");
fragTrans.commit();
return root;
}
}
<<p>子片段/strong>
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
import com.app.ssl.R;
public class NestedFragment extends Fragment {
private View root;
private TextView textview;
private String title;
public NestedFragment(String title) {
this.title = title;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
root =  inflater.inflate(R.layout.fragment_nested, container, false);
textview = root.findViewById(R.id.textView);
textview.setText(title);
return root;
}
}

父布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/ContainerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ui.nestedFragment.ParentFragment">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:scrollbars="none"
android:layout_weight="1">
<LinearLayout
android:id="@+id/nested_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
</LinearLayout>
<<p>孩子布局/strong>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="800sp"
android:layout_margin="20dp"
android:background="#f1ff91"
android:layout_weight="1">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Child fragment"/>
</LinearLayout>

最新更新