简体   繁体   English

如何在片段中创建自定义列表?

[英]How to do a Custom List in Fragment?

I'm working on an application that needs to have a Custom list in a fragment. 我正在开发需要在片段中包含自定义列表的应用程序。 I try a lot of thing in vain... This is my best result: 我徒劳地尝试了很多事情……这是我最好的结果:

MainActivity.java : MainActivity.java

import android.support.v4.app.FragmentActivity;
import android.support.v4.app.ListFragment;
import android.os.Bundle;

public class MainActivity extends FragmentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_list);
        if(findViewById(R.id.fragment_container) != null )
        {
            if(savedInstanceState != null)
                return;
        }
        ListFragment listFragment = new ListFragment();
        listFragment.setArguments(getIntent().getExtras());
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_container, listFragment).commit();
    }
}

stops.java : Stops.java

public class stops {
    private String name;
    private String destination;
    private String schedule;
    private int iconID;

    public stops(String name, String destination, String schedule, int iconID)
    {
        super();
        this.name = name;
        this.destination = destination;
        this.schedule = schedule;
        this.iconID = iconID;
    }
    public String getName(){ return name; }
    public String getDestination(){ return destination; }
    public String getSchedule(){ return schedule; }
    public int getIconID(){ return iconID; }
}

ListFragment.java : ListFragment.java

import android.content.Context;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;

public class ListFragment extends Fragment{
    private List<stops> mystops = new ArrayList<>();
    ArrayAdapter<stops> adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        populateStopList();
        View V = inflater.inflate(R.layout.fragment_list, container, false);
        Context context = getActivity().getApplicationContext();
        adapter = new myListAdapter(context);
        ListView list = (ListView) V.findViewById(R.id.stopsList);
        list.setAdapter(adapter);
        return V;
    }

    private void populateStopList() {
        mystops.add(new stops("Corum", "Odysseum", "5min", R.drawable.green_tram));
        mystops.add(new stops("Corum", "Mosson", "2min", R.drawable.orange_tram));
        mystops.add(new stops("Corum", "Saint-Jean de Vedas", "1min", R.drawable.red_tram));
    }

    private class myListAdapter extends ArrayAdapter<stops> {
        public myListAdapter(Context context) {
            super(context, R.layout.item_view, mystops);
            add(new stops("Corum", "Mosson", "2min", R.drawable.green_tram));
            populateStopList();
            notifyDataSetChanged();
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View itemView = convertView;
            if (itemView == null)
                itemView = getActivity().getLayoutInflater().inflate(R.layout.item_view, parent, false);
            stops currentStop = mystops.get(position);
            ImageView imageView = (ImageView)itemView.findViewById(R.id.item_icon);
            imageView.setImageResource(currentStop.getIconID());
            TextView nameText = (TextView)itemView.findViewById(R.id.item_nameTextView);
            nameText.setText(currentStop.getName());
            TextView destinationText = (TextView) itemView.findViewById(R.id.item_destinationTextView);
            destinationText.setText(currentStop.getDestination());
            TextView timeText = (TextView) itemView.findViewById(R.id.item_timeTextView);
            timeText.setText(currentStop.getSchedule());
            return itemView;
        }
    }
}

fragment_list.xml : fragment_list.xml

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" tools:   context="com.example.msa_m.listsstops.ListFragment"
    android:id= "@+id/fragment_container"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TEXT DU LAYOUT FRAGMENT"
        android:id="@+id/textView"
        android:layout_gravity="center_horizontal|top" />
    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/stopsList"
        android:layout_alignParentEnd="true"
        />
</FrameLayout>

item_view.xml : item_view.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id ="@+id/my_list_item">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/item_icon"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:src="@drawable/green_bus"
        android:maxWidth="80dp"
        android:minHeight="80dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Nom de l&apos;arret"
        android:id="@+id/item_nameTextView"
        android:layout_alignParentTop="true"
        android:layout_toEndOf="@+id/item_icon" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Destination"
        android:layout_marginTop="5dp"
        android:layout_marginStart="10dp"
        android:id="@+id/item_destinationTextView"
        android:layout_below="@+id/item_nameTextView"
        android:layout_toEndOf="@+id/item_icon" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Temps"
        android:id="@+id/item_timeTextView"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="10dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true" />
</RelativeLayout><?xml version="1.0" encoding="utf-8"?>

But when I run it, this endless load with a ProgressBar in the middle of the screen... 但是,当我运行它时,屏幕中间会不断加载ProgressBar。

Does someone can help me ? 有人可以帮我吗?

Ok, since your code works fine for me, heres a modified version of your project. 好的,由于您的代码对我来说正常工作,所以这里是您项目的修改版本。

Your Activity Layout could probably look like: 您的活动布局可能如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <FrameLayout
        android:id="@+id/fragmentContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</RelativeLayout>

Then, in your activity, you just inflate the activity layout and replace the container with your fragment: 然后,在您的活动中,只需膨胀活动布局并用片段替换容器:

public class MainActivity extends FragmentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListFragment listFragment = new ListFragment();
        listFragment.setArguments(getIntent().getExtras());
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragmentContainer, listFragment)
                .commit();
    }
}

And your fragment takes care of inflating the list ui and managing all the data: 您的片段负责填充列表ui和管理所有数据:

public class ListFragment extends Fragment{
    ListView listView;
    ArrayAdapter<stops> adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ArrayList<stops> mystops = new ArrayList<>();
        mystops.add(new stops("Corum", "Odysseum", "5min", R.drawable.green_tram));
        mystops.add(new stops("Corum", "Mosson", "2min", R.drawable.orange_tram));
        mystops.add(new stops("Corum", "Saint-Jean de Vedas", "1min", R.drawable.red_tram));

        adapter = new myListAdapter(getActivity(), mystops);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_list, container, false);

        listView = (ListView) view.findViewById(R.id.stopsList);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        listView.setAdapter(adapter);
    }

    private static class myListAdapter extends ArrayAdapter<stops> {
        public myListAdapter(Context context, ArrayList<stops> stopList) {
            super(context, R.layout.item_view, stopList);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View itemView = convertView;
            if (itemView == null)
                itemView = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_view, parent, false);

            stops currentStop = getItem(position);

            ImageView imageView = (ImageView)itemView.findViewById(R.id.item_icon);
            imageView.setImageResource(currentStop.getIconID());
            TextView nameText = (TextView)itemView.findViewById(R.id.item_nameTextView);
            nameText.setText(currentStop.getName());
            TextView destinationText = (TextView) itemView.findViewById(R.id.item_destinationTextView);
            destinationText.setText(currentStop.getDestination());
            TextView timeText = (TextView) itemView.findViewById(R.id.item_timeTextView);
            timeText.setText(currentStop.getSchedule());

            return itemView;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM