[英]Click events on ListView custom item Android
我正在嘗試實現UI,如listview所示:
ON選擇:
正常:
現在,我還在主要活動的底部有一個面板,無論是否選擇項目,其視圖都會改變。
現在,我要處理點擊事件。 我在主要活動中有一個AdapterView.OnItemClickListener,它工作正常。 但是我想實現一個長按監聽器。 長按后,列表中項目的布局將更改為顯示選擇按鈕。 但是我無法在“列表”視圖中進行多項選擇。
我的活動中沒有操作欄,我想實現以下行為。
我嘗試在視圖上設置單擊偵聽器,但是這樣只會影響一個視圖(在選擇時)。 有人可以指導我如何實現這一目標。
我的清單查看商品代碼:
<?xml version="1.0" encoding="utf-8"?>
<!-- Height will be relative to Icon Size -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- Item -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1138"
android:background="@drawable/navigation_list_bottom_border"
android:orientation="vertical">
<!-- Image Tittle etc-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="103">
<ImageView
android:id="@+id/im_navigation_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="@mipmap/ic_fb_unknown_item"/>
<!-- Title -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="894">
<TextView
android:id="@+id/tv_navigation_item_title"
style="@style/navigation_item_title_style"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="middle"
android:maxLines="1"/>
</LinearLayout>
<!-- Size -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="149">
<TextView
android:id="@+id/tv_navigation_item_size"
style="@style/navigation_item_size_style"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</LinearLayout>
<!-- Additional details like date -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="68.5">
<TextView
android:id="@+id/tv_navigation_item_date"
style="@style/navigation_item_date_style"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</LinearLayout>
<!-- Selection -->
<LinearLayout
android:id="@+id/ll_navigation_list_item_check"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="143"
android:visibility="gone">
<ImageView
android:id="@+id/im_item_not_selected"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="@mipmap/ic_fb_list_item_unchecked"/>
</LinearLayout>
</LinearLayout>
我的主要活動:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/wallpaper_blur_explorer"
android:orientation="vertical"
tools:context=".ExplorerActivity">
<!-- MAIN LAYOUT -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<!--weight = 2035/525 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="525"
android:orientation="vertical">
<include layout="@layout/title_explorer"/>
</LinearLayout>
<!--weight = 2035/525 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2035"
android:background="@android:color/white"
android:orientation="vertical">
<com.micromax.filebrowser.ui.NavigationListView
android:id="@+id/listview_navigation"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null"
android:scrollbars="vertical">
</com.micromax.filebrowser.ui.NavigationListView>
</LinearLayout>
</LinearLayout>
<!-- Overlay for bottom bar-->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- Bottom bar -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_gravity="bottom"
android:alpha="0.9"
android:background="@android:color/holo_blue_dark"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>
</FrameLayout>
現在,我希望在“項目選擇”中更改“底部”面板。 我在Main活動中有一個這樣的聽眾:
private AdapterView.OnItemClickListener mNavItemClickListener =
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
DirInfo dirInfo = (DirInfo) parent.getItemAtPosition(position);
if (dirInfo.isDeviceDir()) {
handleItemClick(position, dirInfo);
} else {
// Item at position 0 is UP navigation
if (position == 0) {
handleUpNavigation(dirInfo);
} else {
handleItemClick(position - 1, dirInfo);
}
}
}
};
現在,我無法弄清楚如何實現所需的行為。 我希望能夠獲得所有選定項目的列表,並能夠在長時間單擊某個項目后選擇多個項目。 長時間單擊也應顯示“選中/未選中的圖像”。
NavigationListView的代碼
public class NavigationListView extends ListView {
private Context mContext;
private NavigationListAdapter mAdapter;
public NavigationListView(Context context) {
super(context);
init(context);
}
public NavigationListView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public NavigationListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
mContext = context;
}
public void startNavigationFromPath(DirInfo dirInfo) {
if (mAdapter == null) {
NavigationListAdapter adapter = new NavigationListAdapter(mContext, dirInfo);
this.setAdapter(adapter);
} else {
mAdapter.setDirectory(dirInfo);
mAdapter.notifyDataSetChanged();
}
}
}
我怎樣才能做到這一點?
1-在我的列表視圖內部項目代碼在底部的Imageview位置添加一個復選框(提供多項選擇功能),以供參考: http ://techlovejump.com/android-listview-with-checkbox/
2-之后,您可能會為復選框創建自定義的選中和未選中視圖,以供參考: http : //androidexample.com/Custom_Checkbox_With_The_Use_Of_Selectors_And_Shapes/index.php? view=article_discription&aid=80&aaid= 104
3-將您的復選框的默認可見性設置為不可見。
4-現在,您可以在setOnItemLongClickListener()
中處理復選框的可見性。 供參考: https : //stackoverflow.com/a/25491769/1384010
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.