一个ListFragment不可点击的按钮内 [英] Buttons within a ListFragment not clickable

查看:237
本文介绍了一个ListFragment不可点击的按钮内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在中选择ActionBar的一个​​选项,勾画出并排在Horizo​​ntalScrollView ListFragments侧(多达用户想要)的活动。

每个ListFragment项目包含TextViews和一个按钮。一个SimpleAdapter在每ListFragment每个项目填充数据。

我现在面临的问题是,在每个列表项的按钮不响应点击。布局可以概括如下:一FragmentActivity内ListFragment内部按钮,在根视图从最里子元件将父

我花了在这个问题上多小时,我无法达成解决办法,使按键响应点击。一些方法我已经使用包括获得按钮的观点和附加onClickListeners,2)实施的ListFragment的OnClickListener接口。我也知道onInterceptTouchEvent方法一类的ViewGroup的,但是我缺乏与Android $ P $经验pvents我从一个解决方案到位。对解决这个问题的任何指导或方向将是最AP preciated。

下面是code为ListFragment:

 包com.example.androidlistfragmenttest;进口的java.util.ArrayList;
进口的java.util.HashMap;
进口android.os.Bundle;
进口android.support.v4.app.ListFragment;
进口android.util.Log;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.view.ViewGroup;
进口android.widget.Button;
进口android.widget.SimpleAdapter;
公共类MyFragment扩展ListFragment {私人的ArrayList<&HashMap的LT;字符串,字符串>> ArrayList的;
@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
                         捆绑savedInstanceState){    //充气的布局该片段    查看查看= inflater.inflate(R.layout.fragment_layout,集装箱,FALSE);
    Button按钮=(按钮)view.findViewById(R.id.button);
    button.setOnClickListener(新OnClickListener(){
       //这不打印logcat中。键没有反应的点击。        @覆盖
        公共无效的onClick(查看为arg0){
            Log.v(哥斯拉,原子BREATH);        }    });
    返回视图。
}@覆盖
公共无效onActivityCreated(捆绑savedInstanceState){
    super.onActivityCreated(savedInstanceState);
    ArrayList的= dataGenerator();
    SimpleAdapter适配器=新SimpleAdapter(getActivity()getApplicationContext(),ArrayList中,R.layout.fragment_layout,新的String [] {KEY},新的INT [] {R.id.text_id});
    setListAdapter(适配器);}
/ *
 *方法来填充适配器的数据表。
 * /公众的ArrayList<&HashMap的LT;字符串,字符串>> dataGenerator(){    HashMap的<字符串,字符串> hashMap1 =新的HashMap<字符串,字符串>();
    hashMap1.put(钥匙,A);    HashMap的<字符串,字符串> hashMap2 =新的HashMap<字符串,字符串>();
    hashMap2.put(钥匙,B);    HashMap的<字符串,字符串> hashMap3 =新的HashMap<字符串,字符串>();
    hashMap3.put(钥匙,C);    HashMap的<字符串,字符串> hashMap4 =新的HashMap<字符串,字符串>();
    hashMap4.put(钥匙,D);    HashMap的<字符串,字符串> hashMap5 =新的HashMap<字符串,字符串>();
    hashMap5.put(钥匙,E);    ArrayList的<&HashMap的LT;字符串,字符串>> ArrayList的=新的ArrayList<&HashMap的LT;字符串,字符串>>();
    arraylist.add(hashMap1);
    arraylist.add(hashMap2);
    arraylist.add(hashMap3);
    arraylist.add(hashMap4);
    arraylist.add(hashMap5);
    返回数组列表;
}} // MyFragment结束

这是code包含的片段(S)的活动:

 包com.example.androidlistfragmenttest;进口的java.util.ArrayList;
进口的java.util.HashMap;
进口java.util.Stack中;
进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.support.v4.app.FragmentActivity;
进口android.support.v4.app.FragmentManager;
进口android.support.v4.app.FragmentTransaction;
进口android.support.v4.app.NavUtils;
进口android.view.Menu;
进口android.view.MenuItem;公共类MainActivity扩展FragmentActivity {    私人堆叠式和LT;弦乐>标签栈;
    私人整数last_tag_number;公共MainActivity(){    last_tag_number =新的整数(0);
    标签栈=新的堆栈<串GT;();
}@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
}@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    //充气菜单;如果是present这增加了项目操作栏。
    。getMenuInflater()膨胀(R.menu.main,菜单);
    返回true;
}@覆盖
公共布尔onOptionsItemSelected(菜单项项){
    开关(item.getItemId()){    案例R.id.add_fragment:
        addColumn();
        返回true;
    案例R.id.remove_column:
        removeColumn();
        返回true;
    案例android.R.id.home:
        //这个ID重新presents主页或向上按钮。在这种情况下
        //活动,向上按钮显示。使用NavUtils允许用户
        //浏览了在应用结构中的一个级别。对于
        //更多详细信息,请参见Android设计的导航模式:
        //
        // http://developer.android.com/design/patterns/navigation.html#up-vs-back
        //
        NavUtils.navigateUpFromSameTask(本);
        返回true;    }
    返回super.onOptionsItemSelected(项目);
}/ *
 *这个方法增加了一个片段到屏幕
 * /公共无效addColumn(){    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    MyFragment片段=新MyFragment();
    fragmentTransaction.add(R.id.fragment_activity,片段,tagGenerator());
    fragmentTransaction.commit();}/ *
 *此方法删除从屏幕片段
 * /公共无效removeColumn(){    如果(tagStack.size()!= 0){
        FragmentManager fragmentManager = getSupportFragmentManager();
        片段片段= fragmentManager.findFragmentByTag(tagStack.pop());
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.remove(片段);
        fragmentTransaction.commit();
    }
}/ *
 *此函数生成标签所显示的屏幕上的每个片段
 *这些标签构成的每个片段作为唯一标识符
 * /公共字符串tagGenerator(){    整数tag_number;    如果(last_tag_number.intValue()== 0){
        tag_number = last_tag_number;
        INT TEMP = last_tag_number.intValue();
        温度+ = 1;
        last_tag_number = Integer.valueOf(临时);
    }
    其他{
        tag_number =新的整数(last_tag_number.intValue());
        INT TEMP = last_tag_number.intValue();
        温度+ = 1;
        last_tag_number = Integer.valueOf(临时);
    }
    字符串标记= tag_number.toString();
    tagStack.push(标签);    返回标签;
}} // MainActivity结束

除了用于FragmentActivity的布局:

 <?XML版本=1.0编码=UTF-8&GT?;< Horizo​​ntalScrollView
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent><的LinearLayout
     机器人:ID =@ + ID / fragment_activity
     机器人:layout_width =FILL_PARENT
     机器人:layout_height =FILL_PARENT
     机器人:方向=横向
     机器人:比重=中心
 >< / LinearLayout中>
< / Horizo​​ntalScrollView>

而ListFragment:

 <?XML版本=1.0编码=UTF-8&GT?;< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =300dp
    机器人:layout_height =match_parent
    机器人:方向=垂直
    机器人:layout_weight =1
    机器人:layout_margin =5DP
    机器人:descendantFocusability =blocksDescendants>< ListView的机器人:ID =@ ID /安卓名单
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
/><的LinearLayout
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=横向
    机器人:layout_gravity =中心>    <的TextView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=@字符串/标签
        机器人:layout_gravity =开始    />    <的TextView
        机器人:ID =@ + ID / text_id
        机器人:layout_width =0dp
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_weight =1
        机器人:layout_gravity =结束
    />< / LinearLayout中><的LinearLayout
    机器人:layout_height =match_parent
    机器人:layout_width =match_parent
    机器人:方向=横向>     <按钮
         机器人:ID =@ + ID /按钮
         机器人:layout_height =WRAP_CONTENT
         机器人:layout_width =WRAP_CONTENT
         机器人:文字=@字符串/按钮
         机器人:layout_margin =2DP
         机器人:layout_marginLeft =2DP
         机器人:layout_marginRight =2DP
      /> < / LinearLayout中>< / LinearLayout中>


解决方案

的问题是,列表项消耗的点击是没有得到传递到下面的按钮。

您需要设置列表项为无效,自己处理的点击次数。在您的自定义适配器添加下面的方法来禁用物品(它们覆盖从BaseAdapter方法​​):

  @覆盖
公共布尔areAllItemsEnabled(){
    返回false;
}@覆盖
公共布尔isEnabled(INT位置){
   返回false;
}

编辑:这是一个相关的问题可能提供更好的解决方案,它取决于你的设计。

I have an Activity that lays out ListFragments side by side (as many as a user wants) in a HorizontalScrollView upon choosing an option in the ActionBar.

Each ListFragment item contains TextViews and a Button. A SimpleAdapter populates data for each item in every ListFragment.

The issue I am facing now is that the buttons in each list item do not respond to clicks. The layout can be summarized as follows: Button inside a ListFragment inside a FragmentActivity, going from the innermost child element to the parent at the root view.

I have spent many hours on this problem and I am unable to arrive at a solution to make the buttons respond to clicks. Some of the approaches I have used include obtaining the button's view and attaching onClickListeners, 2) implementing the OnClickListener interface for the ListFragment. I am also aware of the onInterceptTouchEvent method for a ViewGroup class, however my lack of experience with Android prevents me from arriving at a solution. Any guidance or direction towards solving this problem would be most appreciated.

Here is the code for the ListFragment:

package com.example.androidlistfragmenttest;

import java.util.ArrayList;
import java.util.HashMap;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.SimpleAdapter;


public class MyFragment extends ListFragment {

private ArrayList<HashMap<String,String>> arraylist;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    // Inflate the layout for this fragment

    View view = inflater.inflate(R.layout.fragment_layout, container, false);
    Button button = (Button) view.findViewById(R.id.button);
    button.setOnClickListener(new OnClickListener(){


       //THIS DOES NOT PRINT IN LOGCAT. BUTTON DOES NOT RESPOND TO CLICKS.

        @Override
        public void onClick(View arg0) {
            Log.v("GODZILLA","ATOMIC BREATH");

        }

    });
    return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState){
    super.onActivityCreated(savedInstanceState);
    arraylist = dataGenerator();
    SimpleAdapter adapter = new SimpleAdapter(getActivity().getApplicationContext(), arraylist, R.layout.fragment_layout,new String[]{"KEY"},new int[]{R.id.text_id});
    setListAdapter(adapter);

}


/*
 * Method to populate an adapter's data list.
 */

public ArrayList<HashMap<String,String>> dataGenerator(){

    HashMap<String,String> hashMap1 = new HashMap<String,String>();
    hashMap1.put("KEY", "A");

    HashMap<String,String> hashMap2 = new HashMap<String,String>();
    hashMap2.put("KEY", "B");

    HashMap<String,String> hashMap3 = new HashMap<String,String>();
    hashMap3.put("KEY", "C");

    HashMap<String,String> hashMap4 = new HashMap<String,String>();
    hashMap4.put("KEY", "D");

    HashMap<String,String> hashMap5 = new HashMap<String,String>();
    hashMap5.put("KEY", "E");

    ArrayList<HashMap<String,String>> arraylist = new ArrayList<HashMap<String,String>>();
    arraylist.add(hashMap1);
    arraylist.add(hashMap2);
    arraylist.add(hashMap3);
    arraylist.add(hashMap4);
    arraylist.add(hashMap5);


    return arraylist;
}

} //End of MyFragment

And this is the code for the Activity containing the Fragment(s):

package com.example.androidlistfragmenttest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Stack;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends FragmentActivity {

    private Stack<String> tagStack;
    private Integer last_tag_number;

public MainActivity(){

    last_tag_number = new Integer("0");
    tagStack = new Stack<String>();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

    case R.id.add_fragment:
        addColumn();
        return true;


    case R.id.remove_column:
        removeColumn();
        return true;


    case android.R.id.home:
        // This ID represents the Home or Up button. In the case of this
        // activity, the Up button is shown. Use NavUtils to allow users
        // to navigate up one level in the application structure. For
        // more details, see the Navigation pattern on Android Design:
        //
        // http://developer.android.com/design/patterns/navigation.html#up-vs-back
        //
        NavUtils.navigateUpFromSameTask(this);
        return true;

    }
    return super.onOptionsItemSelected(item);
}

/*
 * This method adds a fragment to the screen    
 */

public void addColumn(){

    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    MyFragment fragment = new MyFragment();
    fragmentTransaction.add(R.id.fragment_activity, fragment,tagGenerator());
    fragmentTransaction.commit();

}

/*
 * This method removes a fragment from the screen
 */

public void removeColumn(){

    if(tagStack.size() != 0){
        FragmentManager fragmentManager = getSupportFragmentManager();
        Fragment fragment = fragmentManager.findFragmentByTag(tagStack.pop());
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.remove(fragment);
        fragmentTransaction.commit();
    }
}

/*
 * This function generates tags for each fragment that is displayed on the screen
 * The tags pose as unique identifiers for each fragment
 */

public String tagGenerator(){

    Integer tag_number; 

    if(last_tag_number.intValue() == 0){
        tag_number = last_tag_number;   
        int temp = last_tag_number.intValue();
        temp+=1;
        last_tag_number = Integer.valueOf(temp);
    }
    else{
        tag_number = new Integer(last_tag_number.intValue());
        int temp = last_tag_number.intValue();
        temp+=1;
        last_tag_number = Integer.valueOf(temp);
    }
    String tag = tag_number.toString();
    tagStack.push(tag);

    return tag;
}



} //End of MainActivity

As well as the layouts for the FragmentActivity:

<?xml version="1.0" encoding="utf-8"?>

<HorizontalScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

>

<LinearLayout  
     android:id="@+id/fragment_activity"
     android:layout_width="fill_parent" 
     android:layout_height = "fill_parent"
     android:orientation = "horizontal"
     android:gravity="center"       
 > 

</LinearLayout>  
</HorizontalScrollView>

And the ListFragment:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="300dp"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:layout_weight="1"
    android:layout_margin="5dp"
    android:descendantFocusability="blocksDescendants" >

<ListView android:id="@id/android:list"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
/> 

<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:orientation="horizontal" 
    android:layout_gravity="center"  >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/label" 
        android:layout_gravity="start"

    />

    <TextView 
        android:id="@+id/text_id"
        android:layout_width="0dp"
        android:layout_height="wrap_content" 
        android:layout_weight="1"
        android:layout_gravity="end"
    />

</LinearLayout>

<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="horizontal" >

     <Button
         android:id="@+id/button"
         android:layout_height="wrap_content"
         android:layout_width="wrap_content"
         android:text="@string/button"
         android:layout_margin="2dp"
         android:layout_marginLeft="2dp"
         android:layout_marginRight="2dp"
      />

 </LinearLayout>

</LinearLayout>

解决方案

The problem is that the ListItem is consuming the click and it is not getting passed to the button below.

You need to set the list items to inactive and handle the clicks yourself. In your custom Adapter add the following methods to disable the items (They override methods from BaseAdapter):

@Override
public boolean areAllItemsEnabled() {
    return false;
}

@Override
public boolean isEnabled(int position) {
   return false;
}

Edit: Here is a related question that may offer a better solution, it depends on your design.

这篇关于一个ListFragment不可点击的按钮内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆