列表视图中的项目不可点击我尝试使用getcurrentfocus()并在content_palce.xml中的relativeLayout标记中阻止后代.
我想从列表中选择的是两个函数:
>当我单击该项目时,将启动另一个活动.
>长按时,会显示上下文菜单.
PlaceActiviy.java
package abdualla.com.covuninavigator;import androID.annotation.TargetAPI;import androID.app.Fragment;import androID.app.FragmentManager;import androID.app.ListActivity;import androID.content.Context;import androID.net.Uri;import androID.os.AsyncTask;import androID.os.Build;import androID.support.design.Widget.floatingActionbutton;import androID.support.design.Widget.NavigationVIEw;import androID.support.design.Widget.Snackbar;import androID.support.v4.Widget.DrawerLayout;import androID.support.v7.app.ActionbarDrawerToggle;import androID.support.v7.app.AppCompatActivity;import androID.os.Bundle;import androID.support.v7.Widget.Toolbar;import androID.text.Editable;import androID.text.TextUtils;import androID.text.TextWatcher;import androID.util.Log;import androID.vIEw.ContextMenu;import androID.vIEw.LayoutInflater;import androID.vIEw.MenuItem;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.AdapterVIEw;import androID.Widget.ArrayAdapter;import androID.Widget.EditText;import androID.Widget.ListVIEw;import androID.Widget.SearchVIEw;import androID.Widget.TextVIEw;import androID.Widget.Toast;import com.Google.androID.gms.appindexing.Action;import com.Google.androID.gms.appindexing.AppIndex;import com.Google.androID.gms.common.API.Googleapiclient;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;//Places Listpublic class PlaceActivity extends AppCompatActivity implements NavigationVIEw.OnNavigationItemSelectedListener { DBHelper db = new DBHelper(this); ListVIEw placesList; EditText seachbar; static listadapter listadapter; static ArrayList<Place> List; /** * ATTENTION: This was auto-generated to implement the App Indexing API. * See https://g.co/AppIndexing/AndroIDStudio for more information. */ private Googleapiclient clIEnt; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { //Un Comment to Build Database for First time run// db.insertPlace("Alan Berry", "Coventry, West MIDlands CV1 5FB", "52.407981", "-1.505527");// db.insertPlace("Coventry Un*iversity College", "113A Gosftord Street Coventry CV15FB", "52.407439", "-1.500221");// db.insertPlace("BUGatti", "Cox Street Coventry CV1 5FB", "52.407479", "-1.503390");// db.insertPlace("Charles Ward", "Cox Street Coventry CV1 5FB", "52.408623", "-1.504856");// db.insertPlace("Engineering & Computing Building", "Gulson Rd, Coventry, West MIDlands CV1 2JH", "52.405607", "-1.499454");// db.insertPlace("Ellen Terry", "Jordan Well,Coventry CV1 5RW", "52.406883", "-1.504703");// db.insertPlace("Frederick Lanchester library", "Frederick Lanchester Building, Coventry University, Coventry, West MIDlands CV1 5DD", "52.406056", "-1.500545");// db.insertPlace("George Elliot", "Coventry CV15LW", "52.408217", "-1.504890");// db.insertPlace("Graham Sutherland", "Gosford Street, Coventry CV1", "52.407212", "-1.502934");// db.insertPlace("The Hub", "Gosford Street, Coventry, West MIDlands CV1 5QP", "52.407741", "-1.504792");// db.insertPlace("Jaguar Building", "Gosford Street, Coventry, West MIDlands CV1 5PJ", "52.407450", "-1.500545");// db.insertPlace("James Starley", "Cox Street, Coventry, West MIDlands CV1 5PH", "52.407892", "-1.504065");// db.insertPlace("Maurice Foss", "Maurice Foss Building, Coventry, West MIDlands CV1 5PH", "52.408047", "-1.503346");// db.insertPlace("Multi-Storey Car Park", "Gosford St, Coventry, West MIDlands CV1 5DD", "52.406288", "-1.499699");// db.insertPlace("Priory Building", "Priory Street, Coventry", "52.407351", "-1.503692");// db.insertPlace("Richard Crossman", "Much Park Street,Coventry, West MIDlands CV1HF", "52.406666", "-1.505438");// db.insertPlace("Sir John Laing Building", "Much Park Street,Coventry, West MIDlands CV1HF", "52.405879", "-1.505003");// db.insertPlace("Sir William Lyons", "Gosford Street, Coventry CV1", "52.407476", "-1.499722");// db.insertPlace("Student Centre", "Gulson Rd, Coventry, West MIDlands CV1 2JH", "52.404984", "-1.500694");// db.insertPlace("Whitefriars", "Coventry CV1 2DS", "52.405255", "-1.501582");// db.insertPlace("William Morris", "Cox Street, Coventry, West MIDlands CV1 5PH", "52.407892", "-1.504065");// db.insertPlace("Sports Centre", "Whitefriars Lane, Coventry, West MIDlands CV1 2DS", "52.405973", "-1.504225");// db.insertPlace("Coventry City Council", "Earl Street, Coventry, West MIDlands CV1 5RR", "52.407355", "-1.508035"); //Un Comment to Build Database for First time run super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_place); Toolbar toolbar = (Toolbar) findVIEwByID(R.ID.toolbar); setSupportActionbar(toolbar); floatingActionbutton fab = (floatingActionbutton) findVIEwByID(R.ID.fab); fab.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw vIEw) { FragmentManager fm = getFragmentManager(); AddplaceFragement addplaceFragement = new AddplaceFragement(); addplaceFragement.show(fm, "Add Place"); } });// DrawerLayout drawer = (DrawerLayout) findVIEwByID(R.ID.drawer_layout);// ActionbarDrawerToggle toggle = new ActionbarDrawerToggle(// this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);// drawer.setDrawerListener(toggle);// toggle.syncState();//// NavigationVIEw navigationVIEw = (NavigationVIEw) findVIEwByID(R.ID.nav_vIEw);// navigationVIEw.setNavigationItemSelectedListener(this);// db.deleteall(); placesList = (ListVIEw) findVIEwByID(R.ID.List); seachbar = (EditText) findVIEwByID(R.ID.searchplaces); List = db.getAllplaces(); listadapter = new listadapter(this, List, getResources()); placesList.setAdapter(listadapter); placesList.setonItemClickListener(new AdapterVIEw.OnItemClickListener() { @OverrIDe public voID onItemClick(AdapterVIEw<?> parent, VIEw vIEw, int position, long ID) { Toast.makeText(getApplicationContext(), "position is:" + position, Toast.LENGTH_LONG); } }); if(placesList.isClickable()){ Log.v("Clickable is ","Yes"); } seachbar.addTextChangedListener(new TextWatcher() { @OverrIDe public voID beforeTextChanged(CharSequence s, int start, int count, int after) { } @OverrIDe public voID onTextChanged(CharSequence s, int start, int before, int count) { listadapter.getFilter().filter(s.toString()); } @OverrIDe public voID afterTextChanged(Editable s) { } }); registerForContextMenu(placesList); // ATTENTION: This was auto-generated to implement the App Indexing API. // See https://g.co/AppIndexing/AndroIDStudio for more information. clIEnt = new Googleapiclient.Builder(this).addAPI(AppIndex.API).build(); } @OverrIDe public boolean onNavigationItemSelected(MenuItem item) { return false; } @OverrIDe public voID onCreateContextMenu(ContextMenu menu, VIEw v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); menu.setheaderTitle("Select an Option"); menu.add(0, v.getID(), 0, "Show on Map"); menu.add(0, v.getID(), 0, "Delete"); } @OverrIDe public boolean onContextItemSelected(MenuItem item) { AdapterVIEw.AdapterContextMenuInfo info = (AdapterVIEw.AdapterContextMenuInfo)item.getMenuInfo(); // info.position will give the index of selected item int IndexSelected=info.position; if(item.getTitle()=="Delete"){ List.remove(IndexSelected); listadapter.notifyDataSetChanged(); } else if (item.getTitle()=="Show on Map"){ Toast.makeText(getApplicationContext(),"Lat is:"+List.get(IndexSelected).getLat()+"lng is: "+List.get(IndexSelected).getLng(),Toast.LENGTH_LONG); } return super.onContextItemSelected(item); } @OverrIDe public voID onStart() { super.onStart(); // ATTENTION: This was auto-generated to implement the App Indexing API. // See https://g.co/AppIndexing/AndroIDStudio for more information. clIEnt.connect(); Action vIEwAction = Action.newAction( Action.TYPE_VIEW, // Todo: choose an action type. "Place Page", // Todo: define a Title for the content shown. // Todo: If you have web page content that matches this app activity's content, // make sure this auto-generated web page URL is correct. // Otherwise, set the URL to null. Uri.parse("http://host/path"), // Todo: Make sure this auto-generated app deep link URI is correct. Uri.parse("androID-app://abdualla.com.covuninavigator/http/host/path") ); AppIndex.AppIndexAPI.start(clIEnt, vIEwAction); } @OverrIDe public voID onStop() { super.onStop(); // ATTENTION: This was auto-generated to implement the App Indexing API. // See https://g.co/AppIndexing/AndroIDStudio for more information. Action vIEwAction = Action.newAction( Action.TYPE_VIEW, // Todo: choose an action type. "Place Page", // Todo: define a Title for the content shown. // Todo: If you have web page content that matches this app activity's content, // make sure this auto-generated web page URL is correct. // Otherwise, set the URL to null. Uri.parse("http://host/path"), // Todo: Make sure this auto-generated app deep link URI is correct. Uri.parse("androID-app://abdualla.com.covuninavigator/http/host/path") ); AppIndex.AppIndexAPI.end(clIEnt, vIEwAction); clIEnt.disconnect(); }}
activity_place.xml
<?xml version="1.0" enCoding="utf-8"?><androID.support.design.Widget.CoordinatorLayout 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:fitsSystemwindows="true" tools:context="abdualla.com.covuninavigator.PlaceActivity"> <androID.support.design.Widget.AppbarLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:theme="@style/Apptheme.AppbarOverlay"> <androID.support.v7.Widget.Toolbar androID:ID="@+ID/toolbar" androID:layout_wIDth="match_parent" androID:layout_height="?attr/actionbarSize" androID:background="?attr/colorPrimary" app:popuptheme="@style/Apptheme.PopupOverlay" androID:title="Places List"/> </androID.support.design.Widget.AppbarLayout> <include layout="@layout/content_place" /> <androID.support.design.Widget.floatingActionbutton androID:ID="@+ID/fab" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_gravity="bottom|end" androID:layout_margin="@dimen/fab_margin" androID:src="@androID:drawable/ic_input_add" /></androID.support.design.Widget.CoordinatorLayout>
content_place.xml
<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" xmlns:app="http://schemas.androID.com/apk/res-auto" androID:orIEntation="horizontal"> <ListVIEw xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:ID="@+ID/List" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_below="@+ID/searchplaces" /> <androID.support.design.Widget.AppbarLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:theme="@style/Apptheme.AppbarOverlay" androID:ID="@+ID/vIEw" /> <EditText androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:ID="@+ID/searchplaces" androID:layout_margintop="47dp" androID:layout_below="@+ID/vIEw" androID:layout_alignParentleft="true" androID:layout_alignParentStart="true" androID:layout_alignParentRight="true" androID:layout_alignParentEnd="true" /></relativeLayout>
listadapter.java
package abdualla.com.covuninavigator;import androID.app.Activity;import androID.content.Context;import androID.content.DialogInterface;import androID.content.res.Resources;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.AdapterVIEw;import androID.Widget.BaseAdapter;import androID.Widget.Filter;import androID.Widget.Filterable;import androID.Widget.ImageVIEw;import androID.Widget.TextVIEw;import java.util.ArrayList;/** * Created by amr on 28/12/15. */public class listadapter extends BaseAdapter implements DialogInterface.OnClickListener, Filterable { private Activity activity; private ArrayList data; private static LayoutInflater inflater=null; public Resources res; Place tempValues=null; int i=0; private ArrayList<Place> mOriginalValues; private ArrayList<Place> mdisplayedValues; public listadapter(Activity a, ArrayList d,Resources resLocal){ this.activity=a; this.data=d; this.res=resLocal; this.mOriginalValues = d; this.mdisplayedValues = d; inflater=(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @OverrIDe public int getCount() { if(data.size()<=0) return 1; return data.size(); } @OverrIDe public Object getItem(int position) { return position; } @OverrIDe public long getItemID(int position) { return position; } @OverrIDe public Filter getFilter() { Filter filter = new Filter() { @SuppressWarnings("unchecked") @OverrIDe protected voID publishResults(CharSequence constraint,FilterResults results) { data = (ArrayList<Place>) results.values; // has the filtered values notifyDataSetChanged(); // notifIEs the data with new filtered values } @OverrIDe protected FilterResults performFiltering(CharSequence constraint) { FilterResults results = new FilterResults(); // Holds the results of a filtering operation in values ArrayList<Place> FilteredArrList = new ArrayList<Place>(); if (mOriginalValues == null) { mOriginalValues = new ArrayList<Place>(mdisplayedValues); // saves the original data in mOriginalValues } /******** * * If constraint(CharSequence that is received) is null returns the mOriginalValues(Original) values * else does the Filtering and returns FilteredArrList(Filtered) * ********/ if (constraint == null || constraint.length() == 0) { // set the Original result to return results.count = mOriginalValues.size(); results.values = mOriginalValues; } else { constraint = constraint.toString().tolowerCase(); for (int i = 0; i < mOriginalValues.size(); i++) { String data = mOriginalValues.get(i).name; if (data.tolowerCase().startsWith(constraint.toString())) { FilteredArrList.add(new Place(mOriginalValues.get(i).getname(),mOriginalValues.get(i).getAddress(),mOriginalValues.get(i).getLat(),mOriginalValues.get(i).getLng())); } } // set the Filtered result to return results.count = FilteredArrList.size(); results.values = FilteredArrList; } return results; } }; return filter; } public static class VIEwHolder{ public TextVIEw name; public TextVIEw address; } @OverrIDe public VIEw getVIEw(int position, VIEw convertVIEw, VIEwGroup parent) { VIEw vi = convertVIEw; VIEwHolder holder; if(convertVIEw==null){ vi=inflater.inflate(R.layout.place_List_item,null); holder=new VIEwHolder(); holder.name=(TextVIEw)vi.findVIEwByID(R.ID.placename); holder.address=(TextVIEw)vi.findVIEwByID(R.ID.address); vi.setTag(holder); } else holder=(VIEwHolder)vi.getTag(); if(data.size()<=0) { holder.name.setText("No Places Found"); holder.address.setText(""); } else{ tempValues=(Place)data.get(position); holder.name.setText(tempValues.getname()); holder.address.setText(tempValues.getAddress()); } vi.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { } }); return vi; } @OverrIDe public voID onClick(DialogInterface dialog, int which) { }}
更新1:
我确实添加了onitemclick监听器,但它没有用.列表可以滚动但不能单击.单击列表视图时会显示反馈..如果你有一个clickListener会发生一些事情….如果你不会发生任何事情,但无论如何它都会检测到点击..这种情况不会发生在我的情况下.似乎没有检测到点击.
解决方法:
添加此代码然后它的工作.
placesList .setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw vIEw) { } });
总结 以上是内存溢出为你收集整理的java – 为什么ListView中的项目不可点击?全部内容,希望文章能够帮你解决java – 为什么ListView中的项目不可点击?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)