我是Android编程的新手,最近尝试使用ListFragment
制作Adapter
。该片段最初旨在显示包含TextView
,ImageButton
和ImageView
的列表。我点击后成功地使ImageButton
成功。但是当我尝试点击ListView
时,它不会这样做。关于这一点,我已经尝试了很多这样的组合。喜欢添加
机器人:可聚焦= “假”
到每个视图,
机器人:descendantFocusability = “blocksDescendants”
到根ListView
布局,但没有一个解决方案适合我。
我的案子还有其他解决办法吗?
event_item_list.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="145dp"
android:orientation="vertical"
android:paddingTop="2.5dp"
android:descendantFocusability="blocksDescendants">
<ImageView
android:layout_width="fill_parent"
android:layout_height="145dp"
android:id="@+id/img_item"
android:scaleType="centerCrop" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="55dp"
android:orientation="horizontal"
android:layout_gravity="bottom"
android:background="#AA000000">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
android:gravity="left"
android:layout_centerVertical="true"
android:paddingLeft="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:id="@+id/txt_item_title"
android:textSize="20sp"
android:text="Title"
android:textAllCaps="true"
android:textStyle="bold"
android:maxLines="1" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/darker_gray"
android:id="@+id/txt_item_date"
android:textSize="15dp"
android:text="Date"
android:maxLines="1" />
</LinearLayout>
<ImageButton
android:layout_width="55dp"
android:layout_height="match_parent"
android:id="@+id/imageButton"
android:layout_gravity="center_vertical"
android:src="@drawable/ic_right_arrow"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:tint="#ffffffff"
android:scaleType="fitXY"
android:drawSelectorOnTop="true"
android:padding="15dp" />
</RelativeLayout>
</FrameLayout>
议程.java:
package com.permata.app.myapplication;
import android.app.ListFragment;
import android.content.Intent;
import android.os.Bundle;
import android.app.Fragment;
import android.support.design.widget.Snackbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import java.util.ArrayList;
/**
* A simple {@link Fragment} subclass.
*/
public class agenda extends ListFragment {
public agenda() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_agenda, container, false);
super.onCreateView(inflater, container, savedInstanceState);
ArrayList<agenda_content> konten_agenda = getKonten();
final ListView lv = (ListView)view.findViewById(android.R.id.list);
lv.setAdapter(new agenda_adapter(this.getActivity(), konten_agenda));
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Intent i = new Intent(getActivity(),agenda_details.class);
startActivity(i);
}
}); //this doesn't work
return view;
}
private ArrayList<agenda_content> getKonten() {
ArrayList<agenda_content> results = new ArrayList<agenda_content>();
agenda_content ct = new agenda_content();
ct.setTitle("Agenda 1");
ct.setDate("Date 1");
ct.setImage(R.drawable.img_sample_1);
results.add(ct);
ct = new agenda_content();
ct.setTitle("Agenda 2");
ct.setDate("Date 2");
ct.setImage(R.drawable.img_sample_2);
results.add(ct);
ct = new agenda_content();
ct.setTitle("Agenda 3");
ct.setDate("Date 3");
ct.setImage(R.drawable.img_sample_3);
results.add(ct);
ct = new agenda_content();
ct.setTitle("Agenda 4");
ct.setDate("Date 4");
ct.setImage(R.drawable.img_sample_1);
results.add(ct);
ct = new agenda_content();
ct.setTitle("Agenda 5");
ct.setDate("Date 5");
ct.setImage(R.drawable.img_sample_2);
results.add(ct);
ct = new agenda_content();
ct.setTitle("Agenda 6");
ct.setDate("Date 6");
ct.setImage(R.drawable.img_sample_3);
results.add(ct);
return results;
}
}
agenda_adapter.java:
package com.permata.app.myapplication;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.RippleDrawable;
import android.media.Image;
import android.support.design.widget.Snackbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class agenda_adapter extends BaseAdapter {
private static ArrayList<agenda_content> content_array;
private LayoutInflater mInflater;
public agenda_adapter(Context context, ArrayList<agenda_content> result) {
content_array = result;
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return content_array.size();
}
@Override
public Object getItem(int position) {
return content_array.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null) {
convertView = mInflater.inflate(R.layout.event_item_list, null);
holder = new ViewHolder();
holder.title = (TextView)convertView.findViewById(R.id.txt_item_title);
holder.date = (TextView)convertView.findViewById(R.id.txt_item_date);
holder.image = (ImageView)convertView.findViewById(R.id.img_item);
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.title.setText(content_array.get(position).getTitle());
holder.date.setText(content_array.get(position).getDate());
holder.image.setImageResource(content_array.get(position).getImage());
ImageButton domore = (ImageButton)convertView.findViewById(R.id.imageButton);
domore.setFocusable(false);
domore.getBackground().setColorFilter(0xAAAC7805, PorterDuff.Mode.MULTIPLY);
domore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Snackbar snackbar = Snackbar.make(v, "HEY!", Snackbar.LENGTH_LONG)
.setAction("Action", null);
snackbar.show();
}
});
return convertView;
}
static class ViewHolder {
TextView title;
TextView date;
ImageView image;
}
}
感谢。
编辑: fragment_agenda.xml包含:
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parrent"
android:id="@id/android:list"
android:drawSelectorOnTop="true"
</ListView>
已解决:
已更改agenda.java以扩展Fragment
而非ListFragment
将fragment_agenda中的Listview
ID设置为@+id/lv_item_agenda
并设置ListView
lv以查找R.id.lv_item_agenda
android.R.id.list
。
但我仍然不知道为什么这种棘手的解决方案有效。我想知道是否有与android.R.id.list
有关的东西让我遇到这个问题。有什么想法吗?
答案 0 :(得分:0)
显然focusable
技巧仅适用于Buttons,并不适用于ImageButtons。请参阅the answer here中的评论。
Android源代码中常用于解决此问题的一件事是名为DontPressWithParentImageView的类。此类用于Android联系人应用中的呼叫按钮。
/**
* Special class to to allow the parent to be pressed without being pressed itself.
* This way the line of a tab can be pressed, but the image itself is not.
*/
public class DontPressWithParentImageView extends ImageView {
public DontPressWithParentImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void setPressed(boolean pressed) {
// If the parent is pressed, do not set to pressed.
if (pressed && ((View) getParent()).isPressed()) {
return;
}
super.setPressed(pressed);
}
}
以下是如何在Java代码中使用它的示例:
if (mCallButton == null) {
mCallButton = new DontPressWithParentImageView(mContext, null);
mCallButton.setId(id);
mCallButton.setOnClickListener(mCallButtonClickListener);
mCallButton.setBackgroundResource(R.drawable.call_background);
mCallButton.setImageResource(android.R.drawable.sym_action_call);
mCallButton.setPadding(mCallButtonPadding, 0, mCallButtonPadding, 0);
mCallButton.setScaleType(ScaleType.CENTER);
}
mCallButton.setTag(tag);
mCallButton.setVisibility(View.VISIBLE);
对你而言,它会是这样的:
DontPressWithParentImageView domore = (DontPressWithParentImageView)convertView.findViewById(R.id.imageButton);
domore.setFocusable(false);
//..............
以下是an example如何在布局xml中使用它:
<com.permata.app.myapplication.DontPressWithParentImageView android:id="@+id/call_button"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:paddingLeft="14dip"
android:paddingRight="14dip"
android:layout_centerVertical="true"
android:gravity="center"
android:src="@android:drawable/sym_action_call"
android:background="@drawable/call_background"
/>