1

我做了一個自定義列表視圖,我在ListFragment。我能夠在我的ListViewAdapter課程中選擇行。但我想讓他們selectabe從我的CompraFragment類擴展ListFragment,並通過選擇一行我想啓動相關的詳細信息頁面。所以我有2個問題:如何使自定義列表視圖可選,並啓動一個新的活動/片段

1)我如何讓我的ListFragment類有可選擇的行,然後通過intents或片段管理器啓動詳細頁面。第二個問題出現在這裏。

2)我應該推出一個詳細頁面作爲FragmetActivity(考慮到我已經有這lauches Fragments和/或ListFragments滑動菜單所以基本上一個3級尋呼系統(菜單 - >項目 - >項目細節))

這裏是我的代碼:

fragment_compra.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <ListView 
     android:id="@+id/android:list" 
     android:layout_width="match_parent" 
     android:descendantFocusability="blocksDescendants" 
     android:layout_height="wrap_content" > 
    </ListView> 

</LinearLayout> 

我自定義的ListView行:listview_item_row.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/linearLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:layout_marginTop="10dp" 
    android:layout_marginBottom="10dp" 
    android:clickable="false" 
     android:focusable="false" 
    android:padding="5dip" > 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/relativeLayout" 
     android:clickable="false" 
     android:focusable="false" 
     android:background="@drawable/list_selector" > 

      <!-- Image dell'ogetto --> 
    <ImageView 
     android:id="@+id/ic_thumbnail" 
     android:layout_width="95dip" 
     android:layout_height="95dip" 
     android:clickable="false" 
     android:focusable="false" 
     android:src="@drawable/ic_thumbnail" /> 

    <!-- Data di scadenza--> 
    <TextView 
     android:id="@+id/tvScadenza" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignTop="@+id/ic_thumbnail" 
     android:layout_toRightOf="@+id/ic_thumbnail" 
     android:gravity="center" 
     android:textColor="#040404" 
     android:typeface="sans" 
     android:textSize="15sp" 
     android:clickable="false" 
     android:focusable="false" 
     android:textStyle="italic"/> 

    <ProgressBar 
     android:id="@+id/hProgressBar" 
     style="@style/CustomProgressBar" 
     android:progressDrawable="@drawable/progressbar_color" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/tvScadenza" 
     android:layout_alignBottom="@+id/ic_thumbnail" 
     android:clickable="false" 
     android:focusable="false" /> 

    <!-- Prezzo --> 
    <TextView 
     android:id="@+id/tvPrezzo" 
     android:layout_width="match_parent" 
     android:layout_height="75dp" 
     android:layout_below="@id/tvScadenza" 
     android:textColor="#343434" 
     android:textSize="30sp" 
     android:gravity="center" 
     android:clickable="false" 
     android:focusable="false" 
     android:layout_toRightOf="@+id/ic_thumbnail" /> 

    <!-- Followers --> 
    <TextView 
     android:id="@+id/tvFollowers" 
     android:layout_width="match_parent" 
     android:layout_height="35dp" 
     android:layout_below="@+id/tvPrezzo" 
     android:textColor="#343434" 
     android:textSize="20sp" 
     android:gravity="center" 
     android:background="@color/light_grey" 
     android:layout_toRightOf="@+id/ic_thumbnail" 
     android:clickable="false" 
     android:focusable="false" 
     android:text="@string/followers" /> 

    <!-- Bottone Segui --> 
    <Button 
     android:id="@+id/btnSegui" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:clickable="false" 
     android:focusable="false" 
     android:background="@drawable/ic_segui_m" 
     android:layout_alignBottom="@+id/tvFollowers" /> 


    </RelativeLayout> 

</LinearLayout> 

ListViewAdapter類

import java.util.ArrayList; 
import java.util.HashMap; 

import android.content.Context; 
import android.graphics.Bitmap; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ImageView; 
import android.widget.ProgressBar; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.prjma.lovertech.R; 
import com.prjma.lovertech.model.Item; 

public class ListViewAdapter extends ArrayAdapter<String> { 

    private static LayoutInflater inflater = null; 

    public Context context; 
    public int layoutResourceId; 
    public ArrayList<HashMap<String, Object>> items; 
    public Bitmap icon; 

    //public ImageLoader imageLoader; 

    public ListViewAdapter(Context context, int listviewItemRow, ArrayList<HashMap<String, Object>> items, Bitmap icon) { 
     // TODO Auto-generated constructor stub 
     super(context, listviewItemRow); 
     this.items = items; 
     this.context = context; 
     this.icon = icon; 
    } 

    public int getCount() { 
     return items.size(); 
    } 

    public Item getItem(Item position) { 
     return position; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     View row = convertView; 

     inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     row = inflater.inflate(R.layout.listview_item_row, null); 

     HashMap<String, Object> item = items.get(position); 

     if (item != null) { 

      ImageView listImage = (ImageView)row.findViewById(R.id.ic_thumbnail); 
      TextView scadenza = (TextView)row.findViewById(R.id.tvScadenza); 
      TextView prezzo = (TextView)row.findViewById(R.id.tvPrezzo); 
      TextView followers = (TextView)row.findViewById(R.id.tvFollowers); 
      ProgressBar hProgressBar = (ProgressBar)row.findViewById(R.id.hProgressBar); 


      if (listImage != null) { 
       listImage.setImageBitmap(icon); 
       listImage.setImageBitmap((Bitmap) item.get("pic1m")); 
      } 

      if (scadenza != null) { 
       scadenza.setText((CharSequence) item.get("scadenza")); 
      } 
      if (prezzo != null) { 
       prezzo.setText((CharSequence) item.get("prezzo")); 
      } 
      if (followers != null) { 
       followers.setText((CharSequence) item.get("followers")); 
      } 
      if (hProgressBar != null) { 
       hProgressBar.setProgress((Integer) item.get("coefficient")); 
      } 
     } 

     //row.onListItemClick(new OnItemClickListener1()); 
     row.setOnClickListener(new OnItemClickListener(position)); 
     return row; 
    } 

    private class OnItemClickListener implements OnClickListener { 

     private int mPosition; 

     private OnItemClickListener(int position){ 
      mPosition = position; 
     } 

     @Override 
     public void onClick(View arg0) { 

      Log.i("onListItemClickList", "Item clicked: " + mPosition); 

      Toast.makeText(context, "Message " + Integer.toString(mPosition), Toast.LENGTH_SHORT).show(); 

     } 
    } 
} 

compraFragment類

import java.util.ArrayList; 
import java.util.HashMap; 

import android.app.ListFragment; 
import android.app.ProgressDialog; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ListView; 
import android.widget.Toast; 

import com.prjma.lovertech.R; 
import com.prjma.lovertech.adapter.ListViewAdapter; 
import com.prjma.lovertech.util.MVPFunctions; 


public class CompraFragment extends ListFragment { 


    public ListView listView; 
    public ListViewAdapter adapter; 

    /** 
    * Keep track of the login task to ensure we can cancel it if requested. 
    */ 
    private DownloadTask mDownloadTask = null; 

    public ArrayList<HashMap<String, Object>> items; 
    public Bitmap icon; 

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

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

     //View rootView = inflater.inflate(R.layout.fragment_compra, false); 
     View rootView = inflater.inflate(R.layout.fragment_compra, container, false); 

     // now you must initialize your list view 
     listView = (ListView) rootView.findViewById(android.R.id.list); 

     mDownloadTask = new DownloadTask(); 
     mDownloadTask.execute((Void) null);  

     return rootView; 
    } 



    /** 
    * Represents an asynchronous login/registration task used to authenticate 
    * the user. 
    */ 
    public class DownloadTask extends AsyncTask<Void, Void, Boolean> { 

     private ProgressDialog progressDialog; 

     @Override 
     protected Boolean doInBackground(Void... params) { 
      // TODO: attempt authentication against a network service. 

      //prendi tutti gli id dal webserver 
      ArrayList<Long> ids = MVPFunctions.getMioSingolo().search(); 

      //per ogni id prendi l'elemento con tutti i valuori di risposta 
      items = new ArrayList<HashMap<String, Object>>(); 
      for(int i=0; i < ids.size(); i++){ 
       items.add(MVPFunctions.getMioSingolo().getItem(ids.get(i))); 
      } 
      return true; 
     } 


     @Override 
     protected void onPreExecute(){ 
      /* 
      * This is executed on UI thread before doInBackground(). It is 
      * the perfect place to show the progress dialog. 
      */ 
      progressDialog = ProgressDialog.show(getActivity(), "Wall", "Downloading Content..."); 
     } 

     @Override 
     protected void onPostExecute(final Boolean success) { 
      mDownloadTask = null; 

      // dismiss the dialog after getting all products 
      progressDialog.dismiss(); 
      //showProgress(false); 

      if (items.get(0).get("status error")!= null){ 
       Toast.makeText(getActivity(), "status error = " + items.get(0).get("status error"), Toast.LENGTH_LONG).show(); 
       Log.i("status error put toast", (String) items.get(0).get("status error")); 
       //fai qualcosa, tipo torna indietro, ecc 
      } 
      // updating UI from Background Thread 
      ListViewAdapter adapter = new ListViewAdapter(getActivity(),R.layout.listview_item_row, items, icon); 
      // updating listview 
      listView.setAdapter(adapter); 
     } 

     @Override 
     protected void onCancelled() { 
      mDownloadTask = null; 
      //showProgress(false); 
     } 
    } 
} 
+1

您可以使用意向開始活動。 – Raghunandan

+0

可以請你告訴我在這種情況下如何? – Pheonix7

回答

1

如果你能看到的物品列表視圖如預期,進行項目,然後雙擊下一最簡單的方法是實現OnItemClickListener爲類。在這裏,

public class CompraFragment extends ListFragment implements OnItemClickListener 

然後添加未實現的方法,然後做的usual-第三個參數會被點擊項目位置和開關-ING它,你打電話給你的下一個活動的意圖。

相關問題