如何在片段中执行自定义列表?

时间:2015-08-18 14:05:57

标签: java android android-fragments

我正在处理需要在片段中包含自定义列表的应用程序。 我徒劳地尝试了很多事情......这是我最好的结果:

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

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

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

<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

<?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"?>

但是当我运行它时,屏幕中间有一个ProgressBar的无尽负载......

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

好的,既然你的代码对我来说很好,那就是你项目的修改版本。

您的活动布局可能如下所示:

<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>

然后,在您的活动中,您只需夸大活动布局并用您的片段替换容器:

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

你的片段负责扩展列表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;
        }
    }