将一个片段称为另一个片段

时间:2017-05-14 12:08:06

标签: android android-fragments

我在ProfileFragment中。当我在ProfileFragment中单击Button时,我想调用BrandFragment。我在BrandFragment中使用Recycler视图。

ProfileFragment.java

 Button btn;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v= inflater.inflate(R.layout.profile_fragment, container, false);
    btn=(Button)v.findViewById(R.id.btn_addVehicle);
    return  v;
}

Profile_fragment.xml

 <Button
    android:id="@+id/btn_addVehicle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="@color/colorPrimaryDark"
    android:textColor="#000"
    android:text="add_vehicle"
    />

BrandFragment.java

    public final  String android_version_names[] = {
        "Donut",
        "Eclair",
        "Froyo",
        "Gingerbread"


};

private final  String android_image_urls[] = {
        "R.drawable.aw",
        "R.drawable.p",
        "R.drawable.p",
        "R.drawable.aw"

};

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v= inflater.inflate(R.layout.fragment_brand, container, false);
    RecyclerView recyclerView = (RecyclerView)v.findViewById(R.id.card_recycler_view);
    recyclerView.setHasFixedSize(true);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
    recyclerView.setLayoutManager(layoutManager);
    ArrayList androidVersions = prepareData();
    BrandAdapter adapter = new BrandAdapter(getContext(),androidVersions);
    recyclerView.setAdapter(adapter);
    return v;
}
   private ArrayList prepareData(){

        ArrayList android_version = new ArrayList<>();
        for(int i=0;i<android_version_names.length;i++){
            AndroidVersion androidVersion = new AndroidVersion();
            androidVersion.setAndroid_version_name(android_version_names[i]);
            androidVersion.setAndroid_image_url(android_image_urls[i]);
            android_version.add(androidVersion);
        }
        return android_version;
    }

BrandAdapter.java

  public class BrandAdapter extends RecyclerView.Adapter<BrandAdapter.ViewHolder>   {
    private ArrayList<AndroidVersion> android_versions;
    private Context context;

    public BrandAdapter(Context context,ArrayList<AndroidVersion> android_versions) {
        this.context = context;
        this.android_versions = android_versions;

    }

    @Override
    public BrandAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_brand, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {

        viewHolder.tv_android.setText(android_versions.get(i).getAndroid_version_name());
        Picasso.with(context).load(android_versions.get(i).getAndroid_image_url()).resize(120, 60).into(viewHolder.img_android);
    }

    @Override
    public int getItemCount() {
        return android_versions.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView tv_android;
        ImageView img_android;
        public ViewHolder(View view) {
            super(view);

            tv_android = (TextView)view.findViewById(R.id.carBrandName);
            img_android = (ImageView)view.findViewById(R.id.carBrandImg);
        }
    }

Androidversion.java

     public class AndroidVersion {
private String android_version_name;
private String android_image_url;

public String getAndroid_version_name() {
    return android_version_name;
}

public void setAndroid_version_name(String android_version_name) {
    this.android_version_name = android_version_name;
}

public String getAndroid_image_url() {
    return android_image_url;
}

public void setAndroid_image_url(String android_image_url) {
    this.android_image_url = android_image_url;
}
}

FragmentBrand.xml

     <LinearLayout android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:orientation="horizontal"
  xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.widget.RecyclerView
    android:id="@+id/card_recycler_view"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</LinearLayout>

row_layout.xml

     <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_margin="10dp"
        android:layout_marginStart="40dp"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/carBrandImg"
            android:layout_width="70dp"
            android:layout_height="match_parent"
            android:src="@drawable/aw"/>

        <TextView
            android:id="@+id/carBrandName"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:paddingLeft="20dp"
            android:text="Renault"
            android:textColor="#2e2727"

            android:textSize="20sp" />
    </LinearLayout>

1 个答案:

答案 0 :(得分:0)

通常情况下,使用Java接口来管理片段交互是一种很好的方法。在这种情况下,您可以在不同的片段之间传递值,而无需费力和简单的方法。