我现在没有问题,但是当我单击列表视图中的项目时,我无法更改活动。 我在列表视图中有一个片段“ HomeFragment”,并且有一个包含适配器的类。
public class HomeFragment extends Fragment {
private static final String TAG ="HomeFragment";
public HomeFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_home, container, false);
Log.d(TAG, "onCreate: Started.");
final ListView mListView = (ListView) view.findViewById(R.id.listview_id);
//CRIAR OS OBJETOS PARA OS PRODUTOS
Produtos computador1 = new Produtos("ROG1", "ASUS", "523,52","drawable://" + R.drawable.ic_home_black_24dp);
Produtos computador2 = new Produtos("ROG2", "ASUS", "853,52","drawable://" + R.drawable.ic_carrinho);
Produtos camara1 = new Produtos("nomecamara1", "lol", "133,82","drawable://" + R.drawable.ic_carrinho);
Produtos computador3 = new Produtos("Predator", "ACER", "942,37","drawable://" + R.drawable.ic_carrinho);
Produtos telemovel1 = new Produtos("IPhone7", "Apple", "723,25","drawable://" + R.drawable.ic_comparar);
Produtos telemovel2 = new Produtos("nokia2", "Nokia", "73,23","drawable://" + R.drawable.ic_conta);
Produtos computador4 = new Produtos("LEGION", "LeNovo", "1005,99","drawable://" + R.drawable.ic_home_black_24dp);
Produtos telemovel3 = new Produtos("nokia5200", "NOKIA", "53,75","drawable://" + R.drawable.ic_conta);
Produtos telemovel4 = new Produtos("P20", "HAWAY", "723,52","drawable://" + R.drawable.ic_carrinho);
//Adicionar os objetos dos produtos a um ARRAYLIST
final ArrayList<Produtos> listaProdutos = new ArrayList<>();
listaProdutos.add(computador1);
listaProdutos.add(computador2);
listaProdutos.add(camara1);
listaProdutos.add(computador3);
listaProdutos.add(telemovel1);
listaProdutos.add(telemovel2);
listaProdutos.add(computador4);
listaProdutos.add(telemovel3);
listaProdutos.add(telemovel4);
//É PRECISO CRIAR UM ADAPTER PERSONALIZADO PORQUE A LIST VIEW VAI TER VARIOS TEXTVIEW POE COLUNA
ProdutosListAdapter adapter = new ProdutosListAdapter(getActivity()
, R.layout.list_adapter_layout, listaProdutos);
mListView.setAdapter(adapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(getActivity(), teste.class);
startActivity(i);
}
});
// Inflate the layout for this fragment
return view; //retorna o inflate do fragmento
}
}
列表视图项的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="horizontal"
android:weightSum="100">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="50"
android:weightSum="100">
<ImageButton
android:id="@+id/img_prod1"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_weight="50" />
<TextView
android:id="@+id/text_prod1"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_weight="50"
android:gravity="center"
android:text="textview1"
android:textAlignment="center"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="50"
android:orientation="vertical">
<TextView
android:id="@+id/text_prod1_p2"
android:layout_width="match_parent"
android:layout_height="30dp"
android:gravity="center"
android:text="textview2" />
<TextView
android:id="@+id/text_prod1_p3"
android:layout_width="match_parent"
android:layout_height="30dp"
android:gravity="center"
android:text="textview3" />
</LinearLayout>
</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="100"
android:background="@android:color/white"
tools:context=".HomeFragment">
<TextView
android:id="@+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:text="@string/title_mais_vendidos"
android:textAppearance="@android:style/TextAppearance.DeviceDefault.Medium"
android:textColor="@android:color/black"
android:textStyle="bold" />
<ListView
android:id="@+id/listview_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/message"
android:layout_marginTop="0dp">
</ListView>
<include layout="@layout/activity_main"></include>
</RelativeLayout>
答案 0 :(得分:0)
您应该在startActivity()意图之后完成当前活动。因此,添加
finish();
startActivity(i)之后;
答案 1 :(得分:0)
在您的适配器中,为每个项目设置onClickListener,例如:
public final static class ListItemViewHolder
extends RecyclerView.ViewHolder {
TextView categoryName;
public ListItemViewHolder(View itemView) {
super(itemView);
categoryName = (TextView) itemView.findViewById(R.id.categoryName);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getActivity(), teste.class);
startActivity(i);
}
});
}
}
答案 2 :(得分:0)
我为自定义点击侦听器创建了一个私有类:
/**
* Item click listener
* */
private class FavoriteClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
FavoriteItem item = favoriteItems.get(position);
Intent iIntent = new Intent(thisContext, DetailActivity.class);
iIntent.putExtra("animal_id", item.getAnimalID());
iIntent.putExtra("api_token", apiToken);
iIntent.putExtra("is_logged_in", true);
startActivityForResult(iIntent, ACTIVITY_PET_DETAIL);
}
}
然后,在onCreateView中(或者通常在onResume中完成该操作),我设置ListView:
mFavoriteList = (ListView) rootView.findViewById(R.id.list_browse);
mFavoriteList.setOnItemClickListener(new FavoriteClickListener());
答案 3 :(得分:0)
ImageButton
正在使用click事件。
尝试在android:focusable="false"
上设置ImageButton
<ImageButton
android:id="@+id/img_prod1"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_weight="50"
android:focusable="false />