2014-11-15 14 views
0

我發現了非常類似的問題,但我沒有找到答案。我試圖阻止滑動滾動和我使用片段的選項卡活動。我正在使用這個https://github.com/johannilsson/android-pulltorefresh,但我得到一個錯誤getListview()它是說方法getListView()是未定義的類型Brottfarir我找到了解決方案,這使用擴展ListActivity,但因爲我擴展片段我不能做那。任何解決方案在android開發中使用片段和ListaActivity的問題

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

+0

發表您的XML佈局 – zozelfelfo

回答

0

你可以嘗試從ListFragment

+0

我想,並沒有奏效。 – user3513207

+0

回答下面是 – Vigen

0

擴展您的片段在你fragment_brottfarir XML你必須有列表視圖。

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

在你的片段,你需要初始化使用片段中https://github.com/johannilsson/android-pulltorefresh LIB其

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

} 
+0

嗨,我試過你的解決方案,它沒有工作。我已經使用我現在的代碼編輯了原始帖子。你可以善良,看看它@Vigen – user3513207

+0

再次編輯的代碼,沒有錯誤,但不幸的應用程序停止,請幫助 – user3513207

0

下面的示例應用程序。

這裏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"}; 

} 

如果有錯誤,請發表您的日誌