在android开发中使用Fragments和ListaActivity的问题

时间:2014-11-15 17:41:33

标签: android listview

我发现了与我所拥有的问题非常相似但未找到答案的问题。我试图阻止滑动滚动,我使用片段进行选项卡式活动。我正在使用这个https://github.com/johannilsson/android-pulltorefresh,但我在getListview()上得到一个错误,它说的是方法getListView()未定义为类型Brottfarir我已经使用扩展ListActivity找到了解决方案但是因为我正在扩展Fragment i不能那样做。任何解决方案?

public class Brottfarir extends Fragment {

private static String url = "http://apis.is/flight?language=en&type=departures";
private static final String TAG_RESULTS = "results";
private JSONArray results = null;
public static ArrayList<Flight> resultsList;
private ListView listView;
private View rootView;

// gerum fligh object fyrir það sem er valið í lista og pos breytur sem heldur utan um position í listview.
private Flight flight;
private Integer pos=null;
private ListView listViewRefresh = null;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {





    rootView = inflater.inflate(R.layout.fragment_brottfarir, container, false);

    // not use this for now, create dummy data until this has been fixed
    //new GetResults().execute();

    resultsList = createDummyFlights();

    MyArrayAdapter adapter = new MyArrayAdapter(getActivity(), resultsList);
    // populate the listView
    listView = (ListView)rootView.findViewById(R.id.list);
    listView.setAdapter(adapter);

    // listener for click
    listView.setOnItemLongClickListener(onListClick);

    // Set a listener to be invoked when the list should be refreshed.
    ((PullToRefreshListView) listView).setOnRefreshListener(new OnRefreshListener() {
        @Override
        public void onRefresh() {
           // Do work to refresh the list here.
            //new GetResults().execute();
            resultsList = createDummyFlights();
        }
    });

    return rootView;
}

这是我的XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#EE1616" >

<TextView android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="Brottfarir123"
    android:textSize="20dp"
    android:layout_centerInParent="true"/>


<ListView 
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#1a2421"
    android:layout_weight="1"
    android:drawSelectorOnTop="false"/>

3 个答案:

答案 0 :(得分:0)

您可以尝试从ListFragment扩展片段

答案 1 :(得分:0)

在你的fragment_brottfarir xml中,你必须有listview。

<ListView android:id="@+id/list"
    ... />

在你的片段中,你需要初始化它的

public class YourFragment extends Fragment {
    private ListView listView = null;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
           Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_brottfarir, container, false);
        listView = (ListView) rootView.findViewById(R.id.list);
        // after this your listview initialized and you can use it.

        ...

        return rootview;
    }

}

答案 2 :(得分:0)

此处示例在片段中使用https://github.com/johannilsson/android-pulltorefresh lib的应用程序。

这里有2个xml文件:第一个用于活动,第二个用于片段

pull_to_refresh.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frame_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

</FrameLayout>

fragment_brottfarir.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#EE1616"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:text="Brottfarir123"
        android:textSize="20dp" />

     <com.markupartist.android.widget.PullToRefreshListView
        android:id="@+id/list"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        />

</RelativeLayout>

您的活动

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

public class PullToRefreshActivity extends FragmentActivity {    

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pull_to_refresh);

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.frame_layout, new Brottfarir());
        ft.commit();

    }
}

和你的片段

import java.util.Arrays;
import java.util.LinkedList;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import com.markupartist.android.widget.PullToRefreshListView;
import com.markupartist.android.widget.PullToRefreshListView.OnRefreshListener;


public class Brottfarir extends Fragment {

    private PullToRefreshListView mListView = null;
    private LinkedList<String> mListItems;

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

        mListView = (PullToRefreshListView) rootView.findViewById(R.id.list);

        // Set a listener to be invoked when the list should be refreshed.
        mListView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                // Do work to refresh the list here.
                new GetDataTask().execute();
            }
        });

        mListItems = new LinkedList<String>();
        mListItems.addAll(Arrays.asList(mStrings));

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, mListItems);

        mListView.setAdapter(adapter);

        return rootView;
    }

    private class GetDataTask extends AsyncTask<Void, Void, String[]> {

        @Override
        protected String[] doInBackground(Void... params) {
            // Simulates a background job.
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {

            }
            return mStrings;
        }

        @Override
        protected void onPostExecute(String[] result) {
            mListItems.addFirst("Added after refresh...");

            // Call onRefreshComplete when the list has been refreshed.
            mListView.onRefreshComplete();

            super.onPostExecute(result);
        }
    }

    private String[] mStrings = {
        "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam",
        "Abondance", "Ackawi", "Acorn", "Adelost", "Affidelice au Chablis",
        "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",
        "Allgauer Emmentaler"};

}

如果您有错误,请发布您的日志