您好,欢迎来到[编程问答]网站首页   源码下载   电子书籍   软件下载   专题
当前位置:首页 >> 编程问答 >> Android >> Viewpager类替代Launcher中的worksapce,遇到焦点无法获得,求高手指点

Viewpager类替代Launcher中的worksapce,遇到焦点无法获得,求高手指点

来源:网络整理     时间:2016/6/5 13:13:02     关键词:

关于网友提出的“Viewpager类替代Launcher中的worksapce,遇到焦点无法获得,求高手指点”问题疑问,本网通过在网上对“Viewpager类替代Launcher中的worksapce,遇到焦点无法获得,求高手指点”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:

问题:Viewpager类替代Launcher中的worksapce,遇到焦点无法获得,求高手指点
描述:

本帖最后由 jayson_jh 于 2012-05-28 14:19:37 编辑

最近在研究用Viewpager类替代Launcher中的worksapce,来达到多个屏幕平滑切换的效果,并且在桌面上直接显示所有的app图标.
如图所示:
Viewpager类替代Launcher中的worksapce,遇到焦点无法获得,求高手指点
但是碰到了一个问题,子view不能获得焦点.查询到需要重写两个函数:onRequestFocusInDescendants,addFocusables.
但重写后仍无法得到焦点,求解.
以下为涉及到的代码:
一个重写Viewpager的Workspace类.
public class Workspace extends ViewPager implements View.OnClickListener{
private ArrayList mGroupList = new ArrayList();
private static String TAG="Workspace";
private int mfocusableScreen=0;
public Workspace(Context context, AttributeSet attrs) {
super(context, attrs);
mGroupList.clear();
setAdapter(mPagerAdpter);
setOnPageChangeListener(new WorkspaceOnPageChangeListener());
}
//workspace data container
private PagerAdapter mPagerAdpter = new PagerAdapter() {
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View)arg1); 
}
@Override
public int getCount() {
return mGroupList.size();
}
public Object instantiateItem(View view, int position) {
((ViewPager) view).addView(mGroupList.get(position));
CellLayout c = (CellLayout)((ViewPager) view).getChildAt(position);
if(c!=null){
int childNum = c.getChildCount();
for(int i=0;i<>
CounterTextView v = (CounterTextView)c.getChildAt(i);
v.setOnClickListener(Workspace.this);
}
}
return mGroupList.get(position);
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
};
@Override  
    public void destroyItem(View container, int position, Object object) {  
         ((ViewPager) container).removeView((View) object);   
    };  
};
PagerAdapter getPagerAdpter(){
return mPagerAdpter;
}
public void addItem(ViewGroup v){
mGroupList.add(v);
}
public void deleteItem(Object o){
mGroupList.remove(o);
}
public ViewGroup getChild(int index){
if(mGroupList.size()>=index){
return mGroupList.get(index);
}
return null;
}
@Override
public void onClick(View arg0) {
final CounterTextView view = (CounterTextView)arg0;
if(view instanceof CounterTextView)
Log.d(TAG, view.getText().toString());
}
@Override
protected boolean onRequestFocusInDescendants(int arg0, Rect arg1) {
//getChild(mfocusableScreen).requestFocus(arg0, arg1);
Log.d(TAG, "onRequestFocusInDescendants");
if(this.getChildCount()>0){
Log.d(TAG, "onRequestFocus screen="+mfocusableScreen);
getChildAt(mfocusableScreen).requestFocus(arg0, arg1);
}
return false;
}
@Override
public void addFocusables(ArrayList arg0, int direction, int arg2) {
// TODO Auto-generated method stub
Log.d(TAG, "addFocusables");
if(this.getChildCount()>0)
{
Log.d(TAG, "addFocusables for child");
getChildAt(mfocusableScreen).addFocusables(arg0, direction);
}
        if (direction == View.FOCUS_LEFT) {
            if (mfocusableScreen > 0) {
             Log.d(TAG, "addFocusables for left page child");
                getChildAt(mfocusableScreen - 1).addFocusables(arg0, direction);
            }
        } else if (direction == View.FOCUS_RIGHT){
            if (mfocusableScreen < getChildCount() - 1) {
             Log.d(TAG, "addFocusables for right page child");
                getChildAt(mfocusableScreen + 1).addFocusables(arg0, direction);
            }
        }
}
@Override
protected void dispatchDraw(Canvas arg0) {
super.dispatchDraw(arg0);
}
class WorkspaceOnPageChangeListener implements OnPageChangeListener{
@Override
public void onPageScrollStateChanged(int arg0) {
//mGroupList.get(arg0).requestFocus();
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
//Log.d(TAG, "onPageScrolled arg0="+arg0+" arg2="+arg2);
}
@Override
public void onPageSelected(int arg0) {
mfocusableScreen =arg0;
Workspace.this.getChildAt(arg0).requestFocus();
}
}
}

ViewPager的初始化函数. CellLayout为Launcher的源码类,用于将ViewGroup 变味格状平均分布.
private void bindApplications() {
        int paperSize = 4*4;
        int paperNum = -1;
        int counter=1;
        int color=0xFFFF0000;
        final int appSize =  mApplications.size();
        LayoutInflater layoutInflter=LayoutInflater.from(this);
        if(mApplications.size()>0)
        {
         paperNum =appSize/paperSize;
        
         paperNum = (appSize-paperNum*paperSize)>0? paperNum+1:paperNum;
         Log.d(TAG_CLASS, "paperNum="+paperNum);
        }
        for(int i=0;i<>
         CellLayout cl = (CellLayout)layoutInflter.inflate(R.layout.workspace_screen, null);
         mWorkspace.addItem(cl);
         final CellLayout cell = (CellLayout)mWorkspace.getChild(i);
         if(cell==null)
         Log.d(TAG_CLASS, "error");
         if(cell instanceof CellLayout)
         Log.d(TAG_CLASS, "GOod");
         for(int j=i*16;j<>
         {
         ApplicationInfo ai = mApplications.get(j);
         CellInfo ci = new CellInfo();
         ci.spanX = 1;
         ci.spanY = 1;
         ci.screen = paperNum;
         int row = (j-(i*16))/4;
         int colunm = Math.abs(4*row -(j-(i*16)));
         ci.cellX = row;
         ci.cellY = colunm;
         CounterTextView appText = (CounterTextView) layoutInflter.inflate(R.layout.application, cl, false);
         appText.setCompoundDrawablesWithIntrinsicBounds(null, ai.icon, null, null);
         appText.setText(ai.title);
         appText.setTag(ai);
         appText.setCounter(counter, color);
         CellLayout.LayoutParams lp =(CellLayout.LayoutParams)cell.getLayoutParams();
         if(lp ==null)
         lp = new CellLayout.LayoutParams(ci.cellX, ci.cellY, 1, 1);
         else
         {
         lp.cellX = ci.cellX;
         lp.cellY = ci.cellY;
         lp.cellHSpan = ci.spanX;
         lp.cellVSpan = ci.spanY;
         }
         cell.addView(appText, -1, lp);
         }
         mWorkspace.getAdapter().notifyDataSetChanged();
         mWorkspace.setCurrentItem(0);
        
        }
    }

以上介绍了“Viewpager类替代Launcher中的worksapce,遇到焦点无法获得,求高手指点”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/1534358.html

相关图片

相关文章