Android listview行上的隐藏/显示按钮单击不工作
我想在第一次单击listview行时显示一个按钮,并在第二次单击时隐藏该按钮。按钮在第一次单击时可见,但在第二次单击时不可见。我尝试了两种方法,使他们都有相同的问题。一个是评论Android listview行上的隐藏/显示按钮单击不工作,android,listview,button,Android,Listview,Button,我想在第一次单击listview行时显示一个按钮,并在第二次单击时隐藏该按钮。按钮在第一次单击时可见,但在第二次单击时不可见。我尝试了两种方法,使他们都有相同的问题。一个是评论 public class ContactsFragment extends Fragment { private ProgressDialog pDialog; List<HashMap<String, String>> fetch2 = new ArrayList<Has
public class ContactsFragment extends Fragment {
private ProgressDialog pDialog;
List<HashMap<String, String>> fetch2 = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser object
JSONParser jsonParser = new JSONParser();
ArrayList<HashMap<String, String>> inboxList;
// products JSONArray
JSONArray inbox = null;
// Inbox JSON url
private static final String INBOX_URL = "http://api.androidhive.info/mail/inbox.json";
// ALL JSON node names
private static final String TAG_MESSAGES = "messages";
private static final String TAG_ID = "id";
private static final String TAG_FROM = "from";
private static final String TAG_EMAIL = "email";
private static final String TAG_SUBJECT = "subject";
private static final String TAG_DATE = "date";
private static final String BU_STRING = "date";
ListView lv;
private Boolean shouldVisible=false;
Button button1 ;
public static final String TAG = LibraryPagerAdapter.class.getSimpleName();
protected JSONArray mTasksData;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.inbox_list, container, false);
System.out.println("inside on create view");
lv = (ListView)rootView.findViewById(R.id.list);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> list, View v, int pos, long id) {
System.out.println("I clicked row item");
button1= (Button)v.findViewById(R.id.button1);
button1.setVisibility(button1.isShown() ? View.GONE : View.VISIBLE);
/*if(!shouldVisible)//IF IT IS` invISIBLE
{
System.out.println("value is"+shouldVisible);
button1.setVisibility(View.VISIBLE);
shouldVisible = false;
}
else //IF IT IS VISIBLE
{
System.out.println(shouldVisible);
button1.setVisibility(View.INVISIBLE);
shouldVisible = true;
}
Toast.makeText(getActivity(), "YOU CLICKED ITEM "+pos,Toast.LENGTH_SHORT).show();*/
}
});
bindListView();
return rootView;
}
public void bindListView() {
new LoadInbox().execute();
}
class LoadInbox extends AsyncTask<String, String, String> {
@SuppressWarnings("static-access")
/*protected void onPreExecute() {
getActivity().setProgressBarIndeterminateVisibility(true);
ProgressDialog progressDialog = new ProgressDialog(getActivity());
progressDialog.show(getActivity(), "Please wait", " loading...");
}*/
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jsonParser.makeHttpRequest(INBOX_URL, "GET",
params);
// Check your log cat for JSON reponse
Log.d("Inbox JSON: ", json.toString());
try {
inbox = json.getJSONArray(TAG_MESSAGES);
// looping through All messages
for (int i = 0; i < inbox.length(); i++) {
System.out.println(inbox.length());
JSONObject c = inbox.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
System.out.println(id);
String from = c.getString(TAG_FROM);
String subject = c.getString(TAG_SUBJECT);
System.out.println(subject);
String date = c.getString(TAG_DATE);
System.out.println(date);
if(subject.length() > 23){
subject = subject.substring(0, 22) + "..";
}
// creating new HashMap
HashMap<String, String> libraryInfo = new HashMap<String, String>();
libraryInfo = new HashMap<String, String>();
// adding each child node to HashMap key => value
libraryInfo.put(TAG_ID, id);
libraryInfo.put(TAG_FROM, from);
libraryInfo.put(TAG_SUBJECT, subject);
libraryInfo.put(TAG_DATE, date);
// adding HashList to ArrayList
fetch2.add(libraryInfo);
System.out.println(fetch2.toString());
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
getActivity().runOnUiThread(new Runnable() {
public void run() {
try{
// dismiss the dialog after getting all products
SimpleAdapter adapter = new SimpleAdapter(getActivity(), fetch2, R.layout.inbox_list_item, new String[] { TAG_FROM, TAG_SUBJECT,BU_STRING },
new int[] { R.id.from, R.id.subject, R.id.button1 });
lv.setAdapter(adapter);
}catch(Exception e){
e.printStackTrace();
}
}
});
getActivity().setProgressBarIndeterminateVisibility(false);
}
protected JSONArray doInBackground(Object... params) {
// TODO Auto-generated method stub
return null;
}
公共类ContactsFragment扩展片段{
私人对话;
List fetch2=new ArrayList();
//创建JSON解析器对象
JSONParser JSONParser=新的JSONParser();
ArrayList inboxList;
//产品JSONArray
JSONArray收件箱=null;
//收件箱JSON url
专用静态最终字符串收件箱_URL=”http://api.androidhive.info/mail/inbox.json";
//所有JSON节点名称
私有静态最终字符串标记_MESSAGES=“MESSAGES”;
私有静态最终字符串标记\u ID=“ID”;
私有静态最终字符串标记_FROM=“FROM”;
私有静态最终字符串标记\u EMAIL=“EMAIL”;
私有静态最终字符串标记_SUBJECT=“SUBJECT”;
私有静态最终字符串标记_DATE=“DATE”;
私有静态最终字符串BU_String=“date”;
ListView lv;
私有布尔值shouldVisible=false;
按钮1;
public static final String TAG=LibraryPagerAdapter.class.getSimpleName();
受保护的JSONArray mTasksData;
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
视图根视图=充气机。充气(R.layout.inbox\u列表,容器,false);
System.out.println(“创建视图时的内部”);
lv=(ListView)rootView.findviewbyd(R.id.list);
lv.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
公共链接(AdapterView列表、视图v、内部位置、长id){
System.out.println(“我单击了行项”);
button1=(按钮)v.findViewById(R.id.button1);
button1.setVisibility(button1.isShown()?View.GONE:View.VISIBLE);
/*if(!shouldVisible)//如果不可见
{
System.out.println(“值为”+shouldVisible);
按钮1.设置可见性(视图.可见);
shouldVisible=false;
}
else//如果它是可见的
{
System.out.println(shouldVisible);
按钮1.设置可见性(视图.不可见);
shouldVisible=true;
}
Toast.makeText(getActivity(),“您单击的项目”+pos,Toast.LENGTH\u SHORT.show()*/
}
});
bindListView();
返回rootView;
}
public void bindListView(){
新建LoadInbox().execute();
}
类LoadInbox扩展异步任务{
@抑制警告(“静态访问”)
/*受保护的void onPreExecute(){
getActivity().setProgressBarIndeterminateVisibility(true);
ProgressDialog=新建ProgressDialog(getActivity());
progressDialog.show(getActivity(),“请稍候”,“正在加载…”);
}*/
受保护的字符串doInBackground(字符串…args){
//建筑参数
List params=new ArrayList();
//从URL获取JSON字符串
JSONObject json=jsonParser.makeHttpRequest(收件箱_URL,“GET”,
参数);
//检查日志cat中的JSON响应
Log.d(“收件箱JSON:,JSON.toString());
试一试{
inbox=json.getJSONArray(标记消息);
//循环浏览所有消息
对于(int i=0;i23){
subject=subject.substring(0,22)+.“;
}
//创建新的HashMap
HashMap libraryInfo=新的HashMap();
libraryInfo=新的HashMap();
//将每个子节点添加到HashMap key=>value
libraryInfo.put(标签ID,ID);
libraryInfo.put(标记从,从);
libraryInfo.put(标记主题,主题);
libraryInfo.put(标签日期、日期);
//将哈希列表添加到ArrayList
fetch2.add(libraryInfo);
System.out.println(fetch2.toString());
}
}捕获(JSONException e){
e、 printStackTrace();
}
返回null;
}
/**
*完成后台任务后,关闭“进度”对话框
* **/
受保护的void onPostExecute(字符串文件\u url){
getActivity().runOnUiThread(新的Runnable()){
公开募捐{
试一试{
//获取所有产品后关闭对话框
SimpleAdapter=new SimpleAdapter(getActivity(),fetch2,R.layout.inbox_列表_项,新字符串[]{TAG_FROM,TAG_SUBJECT,BU_String},
新int[]{R.id.from,R.id.subject,R.id.button1});
低压设置适配器(适配器);
}捕获(例外e){
e、 printStackTrace();
}
}
});
getActivity().setProgressBarIndeterminateVisibility(false);
}
受保护的JSONArray doInBackground(对象…参数){
//TODO自动生成的方法存根
返回null;
}
inbox.list.xml
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:drawSelectorOnTop="false"
android:text="contactslist"
android:textColor="#ffffff"
/>
收件箱列表项.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="wrap_content"
android:orientation="vertical" >
<!-- From Label -->
<TextView
android:id="@+id/from"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="4dip"
android:paddingLeft="8dip"
android:paddingTop="8dip"
android:textColor="#ffffff"
android:textSize="20dip"
android:textStyle="bold" />
<!-- Mail Subject -->
<TextView
android:id="@+id/subject"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/from"
android:paddingBottom="6dip"
android:paddingLeft="8dip"
android:textSize="15dip" />
<!-- Mail date -->
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:padding="8dip"
android:textColor="#fff"
android:text="call"
android:visibility="invisible"
android:textSize="13sp" />
</RelativeLayout>
那你为什么要选按钮呢。? 及 切换按钮的实现是这样的
<ToggleButton
android:id="@+id/edit_toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:background="?attr/selectableItemBackgroundBorderless"
android:textColor="@android:color/white"
android:textOff="EDIT"
android:textOn="DONE"/>
所以,问题在于,您在
列表视图
项上的按钮在其可见且列表视图的onItemClick
不再被触发后会聚焦
修理
<Button
android:focusable="false"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:padding="8dip"
android:textColor="#fff"
android:text="call"
android:visibility="invisible"
android:textSize="13sp" />