作者lulucat (PPcat)
看板AndroidDev
标题[问题] 怎麽让 custom layout可以focus
时间Wed Sep 25 11:30:11 2013
我想要做一个记事软体,做了一个linearlayout,结构如下。
自制linearlayout的名称:a01
______________________
| |
| ____________________ |
|| ||
|| textview || textview显示这个月是哪一天。
||____________________|| (EX: 9/6,那麽texview就会显示 "6" )
| ____________________ |
|| ||
|| || gridview 会显示正方形色块(颜色只是代表事情的紧急度)
|| gridview || ,假设有三个色块,那就代表这天你有三件事情要做。
|| ||
|| ||
|| ||
|| ||
|| ||
|| ||
||____________________||
| |
|______________________|
然後这样一份layout,会在fragment用include包起来使用,会重复35次使用
,排成7*5的形式。
如下:
a01 a01 a01 a01 a01 a01 a01
a01 . . . . . . . . . a01
a01 . . . . . . . . . . .
a01 . . . . . . . . . . .
a01 . . . . . . . . . a01
我想要让每一份a01都能点选,只要能够辨识出手指点到的是哪一份a01就可以了。
目的是想让使用者点选到其中一个a01,会在另一份fragment显示相对应的资料。
我有试过使用focus,但是他只会在我点到textview时有作用,
但是我点gridview时就不会有focus事件发生,请问这如何解?
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.136.130.57
※ 编辑: lulucat 来自: 140.136.130.57 (09/25 11:52)
1F:→ ted66:动态产生的时候给一个ID,这样应该可以抓到 09/25 14:00
我有给每个a01设定ID,因为是包在include里面使用,ID是设给include,再用include
去抓里面的Linearlayout。
现在是可以抓到各个textview的focus,但是因为textview占整个linearlayout一小部分
,手指按的空间太小,我是希望能手指按到整个linearlayout去找focus,问题是,
按gridview不会有反应。
※ 编辑: lulucat 来自: 140.136.130.57 (09/25 14:15)
※ 编辑: lulucat 来自: 140.136.130.57 (09/25 15:39)
2F:推 zxc190:android:descendantFocusability="blocksDescendants" 09/25 22:30
3F:→ zxc190:试试在XML的LinearLayout加上这个属性 09/25 22:31
我加入这段就会出错,而且还发现,我能够找到focus是因为有setOnClickListener,
要是没有监听onclick,那麽我监听focus也无法focus到手指点的textview。
还是有没有其他办法让我达成类似效果,只要能够显示几月几号和色块就可以了,
我想很久都想不出做出这类效果的其他方法。
※ 编辑: lulucat 来自: 140.136.130.57 (09/26 10:21)
以下是我自制layout的code。
我把这段code放到a01.xml,a01.xml是tablelayout,在里面放一个DayTag
然後再fragment不断使用include引用a01.xml,做出7*5的表格。
public class DayTag
extends LinearLayout {
private static final String tag =
"DayTag.java";
private Context
context;
private TextView
day;
private GridView
tagview;
private GridAdapter
gridAdapter;
public DayTag(Context context) {
super(context);
this.
context = context;
findview();
setting();
}
public DayTag(Context context, AttributeSet attrs) {
super(context, attrs);
findview();
setting();
}
public DayTag(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.
context = context;
findview();
setting();
}
//设定非本月的日子,画面成淡色
public void setNotDayOfThisMonth(){
setAlpha((
float) 0.5);
}
public void setCalenderTag(ArrayList<MonthCalender> list) {
ArrayList<Tag> taglist =
new ArrayList<Tag>();
for (MonthCalender i : list) {
Tag t =
new Tag(i.getAccount(),
i.getYear(), i.getMonth(), i.getDay(),
i.getId(),i.getTag_color(), i.getIsComplete());
taglist.add(t);
}
gridAdapter.setTag(taglist);
}
public void setSize(int size) {
gridAdapter.setSize(size);
}
public void setText(String msg) {
day.setText(msg);
}
//基本设定
public void setting() {
setBackgroundResource(R.color.
white);
setFocusable(
true);
setFocusableInTouchMode(
true);
setDescendantFocusability(
FOCUS_BLOCK_DESCENDANTS);
tagview.setNumColumns(10);
ViewGroup.LayoutParams layoutParams
=
tagview.getLayoutParams();
layoutParams.
height = 130;
}
public void findview() {
day =
new TextView(
context);
tagview =
new GridView(
context);
gridAdapter =
new GridAdapter(
context);
tagview.setAdapter(
gridAdapter);
addView(
day);
addView(
tagview);
day.setText(
"请输入日期");
}
private class GridAdapter
extends BaseAdapter {
private Context
context;
private ArrayList<Tag>
list =
new ArrayList<Tag>();
private int size;
public GridAdapter(Context context) {
this.
context = context;
}
public void setSize(int size) {
this.
size = size;
}
public void setTag(ArrayList<Tag> list) {
this.
list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(
int position) {
return null;
}
@Override
public long getItemId(
int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView iv = (ImageView) convertView;
if (iv == null) {
iv = new ImageView(context);
iv.setLayoutParams(new GridView.LayoutParams(size, size));
iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
iv.setPadding(2, 2, 2, 2);
}
//判断是否完成,决定标签的显示
if(list.get(position).getIsComplete() == 0) { //未完成
iv.setImageResource(
Integer.parseInt(
list.get(position)
.getTag_color()));
}
else { //已完成
iv.setImageResource(R.color.
black);
}
return iv;
}
}
private class Tag {
private String
account;
private int year;
private int month;
private int day;
private int id;
private String
tag_color;
private int isComplete;
.
.
.
.
}
}
※ 编辑: lulucat 来自: 140.136.130.57 (09/26 12:09)
※ 编辑: lulucat 来自: 140.136.130.57 (09/26 12:32)
※ 编辑: lulucat 来自: 140.136.130.57 (09/26 12:36)
※ 编辑: lulucat 来自: 118.161.252.31 (09/26 12:58)
5F:推 tac0wu:用onTouchEvent 硬干也不是不行 XDD 09/26 22:42
6F:推 tac0wu:直接在你的DayTag上设click listener会有甚麽问题吗? 09/26 23:00
我试着使用onTouchEvent,但是这跟之前发生的情况还是一样,只对textview有反应,
所以我想就还是先用onTouchEvent,至於gridview点选不到的问题就先不管了,
不然我的东西不会有进度。
但还是希望有人能提出解法,我想是必须重整个layout着手,只是目前我想不到要怎麽弄
。
※ 编辑: lulucat 来自: 118.161.252.31 (09/27 22:13)