Android:如何将焦点带到更新的视图

时间:2013-12-02 04:44:15

标签: android android-linearlayout android-view focusmanager

我在3行(LinearLayout)的视图中加载数据,每行有3 TextView,如果焦点位于行的最后TextView,则更新相同的视图和KeyEvent.KEYCODE_DPAD_RIGHT

我可以更新KeyEvent.KEYCODE_DPAD_RIGHT上的视图,但焦点不会回到更新视图的同一行。

问题:当更新视图时,我希望焦点保持在更新视图的同一行中,

。 。

  <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/linearlayout03" >

            <LinearLayout
                android:id="@+id/linearLayout1"
                android:layout_width="fill_parent"
                android:layout_height="74dp"
                android:background="@drawable/background" >
            </LinearLayout>

            <LinearLayout
                android:id="@+id/linearLayout2"
                android:layout_width="fill_parent"
                android:layout_height="74dp"
                android:background="@drawable/background" >
            </LinearLayout>

            <LinearLayout
                android:id="@+id/linearLayout3"
                android:layout_width="fill_parent"
                android:layout_height="74dp"
                android:background="@drawable/background" >
            </LinearLayout>
 </LinearLayout>

。 。

填充线性布局的行

 public void listfill(final LinearLayout ll, CustTextView[] tv, final List<Eventinfo> name, final int curf) {
        final List<Eventinfo> nameref = name;
            ll.removeAllViews();
            if (name.size() == 0) {
                name.add(new Eventinfo(true));
                name.add(new Eventinfo(true));
                name.add(new Eventinfo(true));
            }
            tv = new CustTextView[name.size()];
            for (int i = 0; i < name.size(); i++) {
                final Eventinfo evinfo = name.get(i);
                Utils.println("ev info: "+evinfo);

                tv[i] = new CustTextView(getApplicationContext());
                LinearLayout.LayoutParams params2;
                if (i != (name.size()-1)){
                    int x = 0;
                    if (i == 0){
                        tv[i].setViewLeft(true);
                        if (!name.get(0).isEmpty)
                            x = (int) calwidth(name.get(0).getEtime());
                        else 
                            x = (int) calwidth(name.get(0).getDur() + 0);
                    }else
                        x = (int) calwidth(name.get(i).getDur() + 0);

                        params2 = new LinearLayout.LayoutParams(
                                (int) x, 74);
                }
                else{
                    tv[i].setViewRight(true);
                    params2 = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 74);
                }
                tv[i].setLayoutParams(params2); 
                tv[i].setText(name.get(i).getName());
                tv[i].setTextColor(Color.WHITE);
                System.out.println(" .... " + name.get(i).getName() + i);
                final int z = i;

                tv[i].setOnFocusChangeListener(new methodOnFocusinlistfill(i, curf, nameref.get(i), tv[i].getisViewRight(), tv[i].getisViewLeft()));

                tv[i].setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        if(z == 0){
                            String url = proxy.getUrl(gevinfo.getCid());
                            // call the tune channel API..
                            mediaPlayer.tune(url);
                        }
                    }
                });
                ll.addView(tv[i]);
            }
}

调用上述方法来填充(更新)下面给出的关键事件视图

  case KeyEvent.KEYCODE_DPAD_RIGHT:       
  if (( cur_focus == 8 || cur_focus == 9 || cur_focus == 10 ) && newRight //to check if it is the last textview){
                stTime.add(Calendar.HOUR,1);
                stTime.add(Calendar.MINUTE,30);
                enTime.add(Calendar.HOUR,1);
                enTime.add(Calendar.MINUTE,30);

                getStimeAndEtime();// will gives the start and end time 
                getEventinforclist();// based on start and end time it will get the data for the respective time duration and calls fill layout to update the view
            }

请提前帮助和谢谢。

2 个答案:

答案 0 :(得分:1)

尝试在视图上调用requestFocus方法。

    view.requestFocus();

要使此方法起作用,您的视图需要具有可调焦性。将该视图的focusable属性设置为xml或以编程方式设置为true。

答案 1 :(得分:0)

感谢@ sayed.jalil的帮助, 上述代码中的更改包含在.xml文件中

      android:descendantFocusability="afterDescendants"
      android:focusable="false"

在所有三个LinearyLayout中,以便焦点移动到子视图,这是TextViews, 并包含新方法'returnfocus()'以根据当前焦点请求焦点,该焦点在

中调用
case KeyEvent.KEYCODE_DPAD_RIGHT:       
if (( cur_focus == 8 || cur_focus == 9 || cur_focus == 10 ) && newRight //to check if it is the     last textview){
            stTime.add(Calendar.HOUR,1);
            stTime.add(Calendar.MINUTE,30);
            enTime.add(Calendar.HOUR,1);
            enTime.add(Calendar.MINUTE,30);

            getStimeAndEtime();// will gives the start and end time 
            getEventinforclist();// based on start and end time it will get the data for the respective time duration and calls fill layout to update the view
            returnfocus();//new method added to retain the focus in same row
        }

和returnfocus()方法是,

public void returnfocus(){
    System.out.println("current focus in returnfocus: "+cur_focus);

    if (copycur == 8){
        l1.requestFocus();
    }else if (copycur == 9){
        l2.requestFocus();
    }else if (copycur == 10){
        l3.requestFocus();
    }
}