如何使自定义列表视图可选择并启动新的活动/片段

时间:2013-12-19 16:50:50

标签: android listview android-intent fragment android-listfragment

我已经制作了自定义列表视图,我在ListFragment中创建了它。我能够在我的ListViewAdapter课程中选择行。但是我想让他们从我的CompraFragment类中选择ListFragment并选择一行我想要启动相对详细信息页面。所以我有两个问题:

1)如何使我的ListFragment类具有可选行,然后通过意图或片段管理器启动详细信息页面。这是第二个问题;

2)我应该以{{1​​}}或Fragmet推出详细信息页面(考虑到我已经有一个幻灯片菜单,其中包含Activity和/或Fragments。所以基本上是3级分页系统(菜单 - >项目 - >项目细节))

这是我的代码:

fragment_compra.xml

ListFragments

我的自定义listView行:<?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_item_row.xml

ListViewAdapter类

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

compraFragment class

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

        }   
    }
}

1 个答案:

答案 0 :(得分:1)

如果您可以按预期看到包含项目的列表视图,那么执行项目单击的下一个最简单的方法是为该类实现OnItemClickListener。这里,

public class CompraFragment extends ListFragment implements OnItemClickListener

然后添加未实现的方法,然后像往常一样 - 第三个参数将是单击的项目位置并切换它,您调用您的Intent进行下一个Activity。