[英]Problems using Fragments and ListaActivity in android development
我發現的問題與我遇到的問題非常相似,但沒有找到答案。 我試圖強加滑動滾動和我使用選項卡式活動的片段。 我正在使用這個https://github.com/johannilsson/android-pulltorefresh但我在getListview()上遇到了一個錯誤,這就是說對於類型Brottfarir的方法getListView()未定義,我已經找到了使用擴展ListActivity的解決方案但是當我擴展Fragment時,我無法做到這一點。 有什么解決辦法嗎?
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"/>
您可以嘗試從ListFragment擴展您的片段
在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;
}
}
在這里,示例應用程序在片段內使用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"};
}
如果有錯誤,請發布日志
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.