gpt4 book ai didi

android - 如何使自定义 ListView 可选择并启动新的 Activity/fragment

转载 作者:行者123 更新时间:2023-11-30 03:12:38 25 4
gpt4 key购买 nike

我制作了一个自定义 ListView ,并将其放在 ListFragment 中。我能够在我的 ListViewAdapter 类中选择行。但我想让它们从扩展 ListFragmentCompraFragment 类中选择,并通过选择一行我想启动相关详细信息页面。所以我有两个问题:

1) 如何使我的 ListFragment 类具有可选择的行,然后通过 Intent 或 fragment 管理器启动详细信息页面。第二个问题来了;

2) 我应该将详细信息页面作为 Fragmet 还是 Activity 启动(考虑到我已经有一个幻灯片菜单可以启动 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);
}
}
}

最佳答案

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

public class CompraFragment extends ListFragment implements OnItemClickListener

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

关于android - 如何使自定义 ListView 可选择并启动新的 Activity/fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20687275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com