setOnClickListener未使用自定义适配器和自定义ListView

时间:2015-05-23 21:03:54

标签: java android listview android-listview

我已经花了几个小时解决问题,但我仍然无法理解。单击ListView中的项目时,HotelOverviewFragment中的setOnClickListener未触发。但是,setOnClickListener可以使用我的自定义适配器(NowArrayAdapter)。

我的问题是:为什么setOnClickListener无法在HotelOverviewFragment(显示ListView的类)中工作?

以下列出了我尝试过的内容:

  • 在hotel_row_layout.xml和fragment_hotel_overview.xml中设置android:focusable="false"android:focusableInTouchMode="false"android:descendantFocusability="blocksDescendants"

  • android:clickable设置为true和false。两者都不起作用。

  • 从BaseAdapter实现更改为arrayAdapter。

  • 我在HotelOverviewFragment尝试了不同的听众:setOnClickListenersetOnItemClickListenersetOnItemSelectedListenersetOnTouchListener。不幸的是,这些都不适合我。

这是我的代码:

自定义适配器

    public class NowArrayAdapter extends ArrayAdapter<String> {

private Context context;
private ArrayList<String> values;
private Typeface typeface;
private static Hashtable fontCache = new Hashtable();
private LayoutInflater inflater;
private TextView item;

public NowArrayAdapter(Context context, ArrayList<String> commandsList) {
    super(context, R.layout.hotel_row_layout, commandsList);
    this.context = context;
    values = new ArrayList<String>();
    values.addAll(commandsList);

    typeface = getTypeface(this.context, "fonts/Roboto-Light.ttf");
    inflater = LayoutInflater.from(this.context);
}

static Typeface getTypeface(Context context, String font) {
    Typeface typeface = (Typeface)fontCache.get(font);
    if (typeface == null) {
        typeface = Typeface.createFromAsset(context.getAssets(), font);
        fontCache.put(font, typeface);
    }
    return typeface;
}

public View getView(int position, View convertView, ViewGroup parent) {

    String myText = getItem(position);

    if(convertView == null) {
        convertView = inflater.inflate(R.layout.hotel_row_layout, parent, false);

        item = (TextView) convertView.findViewById(R.id.maps_button);
        item.setTypeface(typeface);

        convertView.setTag(item);
    } else {
        item = (TextView) convertView.getTag();
    }

    item.setText(myText);
    //myListItem.descText.setTextSize(14);

    convertView.setOnClickListener(new View.OnClickListener() {
        // WORKS!
        @Override
        public void onClick(View view) {
            Log.d("click", "Don't look at me!");
        }
    });

    return convertView;
   }

}

片段

    public class HotelOverviewFragment extends Fragment {
private static Hashtable fontCache = new Hashtable();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_hotel_overview, container, false);
    ListView list = (ListView) v.findViewById(android.R.id.list);

    // Set up listview and buttons
    setUp(v, list);

    // Inflate the layout for this fragment
    return v;
}

static Typeface getTypeface(Context context, String font) {
    Typeface typeface = (Typeface)fontCache.get(font);
    if (typeface == null) {
        typeface = Typeface.createFromAsset(context.getAssets(), font);
        fontCache.put(font, typeface);
    }
    return typeface;
}

public void setUp(View v, ListView l){
    TextView address = (TextView) v.findViewById(R.id.content);
    TextView header = (TextView) v.findViewById(R.id.header);
    TextView hotelName = (TextView) v.findViewById(R.id.hotelName);
    Typeface typeface = getTypeface(getActivity(), "fonts/Roboto-Light.ttf");

    address.setText("some street \nZipCode, City \nCountry \nEmail \nphoneNumber");
    address.setTypeface(typeface);

    header.setText("Hotel info");
    header.setTypeface(typeface);
    header.setTextSize(20);

    hotelName.setText("Hotel name");
    hotelName.setTypeface(typeface);

    // Set up button
    ArrayList<String> n = new ArrayList<String>();
    n.add(0, "More info");

    // Show button
    NowArrayAdapter adapter = new NowArrayAdapter(getActivity(), n);

    l.setAdapter(adapter);

    // THIS IS THE STUFF I'VE BEEN TRYING
    try {
        l.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("click", "Success");
            }
        });

        l.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.d("click", "Success");
            }
        });

        l.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Log.d("click", "Success");
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        l.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.d("click", "Success");

                return false;
            }
        });
    }catch (Exception e){
        Log.d("click", e + "");
    }
}


}

HotelOverviewFragment的布局xml

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.example"
android:background="#ffebebeb">

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

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

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

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageView
                    android:layout_width="fill_parent"
                    android:layout_height="200dp"
                    android:src="@drawable/banner"
                    android:id="@+id/hotelBanner"
                    android:layout_gravity="top"
                    android:adjustViewBounds="false"
                    android:scaleType="fitXY" />

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:text="Hotelname"
                    android:id="@+id/hotelName"
                    android:gravity="center"
                    android:textSize="40dp"
                    android:layout_alignBottom="@+id/hotelBanner"
                    android:layout_alignParentRight="false"
                    android:layout_alignParentLeft="false"
                    android:textColor="#ffc4c4c4"
                    android:layout_marginBottom="5dp" />
            </RelativeLayout>
        </LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="0dp"
            android:background="@drawable/header_card">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <TextView
                    android:id="@+id/header"
                    android:layout_gravity="center_vertical"
                    android:layout_width="wrap_content"
                    android:layout_weight="1"
                    android:textColor="@android:color/primary_text_light"
                    android:layout_height="wrap_content"
                    android:text="Header"
                    android:layout_toRightOf="@+id/headerImage"
                    android:layout_centerVertical="true" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/headerImage"
                    android:src="@drawable/ic_action_live_help"
                    android:layout_centerVertical="true" />
            </RelativeLayout>

        </LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="0dp"
            android:layout_marginBottom="0dp"
            android:background="@drawable/content_card">

            <TextView
                android:id="@+id/content"
                android:layout_gravity="left|center_vertical"
                android:layout_width="fill_parent"
                android:layout_weight="1"
                android:textColor="@android:color/primary_text_light"
                android:layout_height="wrap_content"
                android:text="Content"
                />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp">

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

        </LinearLayout>

    </LinearLayout>

</ScrollView>

列表视图项的自定义xml

 <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="6dp"
    android:layout_marginRight="6dp"
    android:layout_marginTop="4dp"
    android:layout_marginBottom="4dp"
    android:background="@drawable/content_card">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/maps_button"
            android:layout_gravity="left|center_vertical"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:textColor="@android:color/primary_text_light"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_alignParentLeft="true" />

        <ImageView
            android:src="@drawable/ic_action_arrow_right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView"
            android:layout_alignParentTop="false"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="false"
            android:layout_centerVertical="true"
            />

    </RelativeLayout>
</LinearLayout>

提前致谢。     

1 个答案:

答案 0 :(得分:0)

感谢Daniel Nugent的建议我得到了它的工作。删除convertView.setOnClickListener()是答案的一部分。我认为它阻止了HotelOverviewFragment中的其他听众。

我的下一个错误是我在ListView上使用setOnClickListener进行测试。 setOnClickListener应该用于按钮而不是ListViews。 所以在删除setOnClickListener后,所有其他听众都开始工作了。

感谢您的时间。