[問題] 怎麼讓 custom layout可以focus
我想要做一個記事軟體,做了一個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)
→
09/25 14:00, , 1F
09/25 14:00, 1F
我有給每個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)
推
09/25 22:30, , 2F
09/25 22:30, 2F
→
09/25 22:31, , 3F
09/25 22:31, 3F
我加入這段就會出錯,而且還發現,我能夠找到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)
推
09/26 22:34, , 4F
09/26 22:34, 4F
推
09/26 22:42, , 5F
09/26 22:42, 5F
推
09/26 23:00, , 6F
09/26 23:00, 6F
我試著使用onTouchEvent,但是這跟之前發生的情況還是一樣,只對textview有反應,
所以我想就還是先用onTouchEvent,至於gridview點選不到的問題就先不管了,
不然我的東西不會有進度。
但還是希望有人能提出解法,我想是必須重整個layout著手,只是目前我想不到要怎麼弄
。
※ 編輯: lulucat 來自: 118.161.252.31 (09/27 22:13)