홈>
지금 콘데 문제는 없지만 목록보기에서 항목으로 클릭하면 활동을 변경할 수 없습니다. 목록보기가있는 "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>
- 답변 # 1
- 답변 # 2
startActivity () 의도 후 현재 활동을 완료해야합니다. 따라서 추가
와이즈 비즈startActivity (i) 후;
- 답변 # 3
어댑터에서 다음과 같이 모든 항목에 대해 onClickListener를 설정하십시오.
finish();
- 답변 # 4
맞춤 클릭 리스너를위한 개인 클래스를 만듭니다 :
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); } }); } }
그런 다음 onCreateView에서 (또는 일반적으로 onResume에서 다운합니다) ListView를 설정합니다 :
/** * 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); } }
mFavoriteList = (ListView) rootView.findViewById(R.id.list_browse); mFavoriteList.setOnItemClickListener(new FavoriteClickListener());
관련 자료
- java - Android, ListView는 데이터를 표시하지 않습니다
- flutter - ListVIew가 다시 빌드 된 후 올바른 오프셋 위치로 스크롤되지 않습니다
- java - 화면에 ListView가 표시되지 않습니다
- c# - HasUnevenRows가있는 목록보기는 셀 높이를 자동으로 변경하지 않습니까?
- android - setOnItemClickListener () 메소드가 조각으로 작동하지 않습니다
- wpf - ListView에 Itemsource의 항목이 표시되지 않습니다
- xamarin.forms - ObservableCollection과 함께 사용 된 Listview는 같은 페이지에서 삭제할 때 마지막 항목을 제대로로드하지 않습니다
- android 내 목록보기에 모든 항목이 표시되지 않습니다
- c# - 콤보 상자가 ListView 내에서 열리지 않습니다
- java - Arrayadapter로 만든 listView에서 setOnItemClickListener를 수행하십시오
관련 질문
- android : BroadcastReceiver에서 ListView를 새로 고치는 방법은 무엇입니까?
- android : (Flutter) ListView에 이미지 만 포함 된 항목 사이에 불필요한 공백이 있습니다.
- Android의 listView에서 선택한보기를 얻는 방법은 무엇입니까?
- android : hasSize 렌더링 문제를 일으키는 ListView 아래에 버튼 또는 콘텐츠 추가
- android : Flutter 앱, ListView.builder는 처음 실행할 때 오류를 표시합니다. 앱을 다시 시작해야합니다 (ListView.builder 및 image_picker).
- Android에서 프로그래밍 방식으로 여러 ListView 생성
- android : Flutter 애플리케이션의 다중 목록 작성기
ImageButton
클릭 이벤트를 소비하고 있습니다.android:focusable="false"
설정 당신의ImageButton
에