0

下面的屏幕快照说明了一个显示五个项目的列表视图,每个项目由两部分组成:A 和 B。列表视图最初显示这五个项目,默认情况下隐藏每个项目的 B 部分。用户应该能够单击 A 部分以显示或隐藏相应的 B 部分。如果用户点击第五个项目,我想将整个列表视图向上移动到 5 以上(包括 A 部分)以腾出空间第五项 B 部分。或者如果你想显示第四个项目的 B 部分,在点击第四个项目后我想向下移动项目 5 为项目 4 的 B 部分腾出空间。底线,如果我们点击一​​个项目,我们想要移动列表视图的位置,以确保我们有空间容纳新暴露的部分 B。

我知道这listview.setSelection(3)会将定位的项目滚动到列表的顶部。而且我也很熟悉listview.smoothScrollToPosition(8),但是这个方法需要api 8,但是我们的项目使用的是版本7。

我在上述动画方面取得了一些进展,但无法确保列表视图项保持(或滚动)到正确的位置。当我扩展和收缩特定项目时,如何控制更广泛的列表视图的位置?

B 部分隐藏然后显示的示例屏幕快照


这是代码:

/**
 * 类SubCategoryAdapter.java的实现描述:TODO 类实现描述
 * @author xianyi.chengxy 2012-8-17 下午7:39:47
 */
public class SubCategoryAdapter extends BaseListAdapter<IndustryNavigationData> {
    private Context mContext ;
    private LayoutInflater mInflater ;
    private ListView mListView ;

    static String defaultName = "subCategory.png" ;

    private int mPreVisiPosition = -1 ;
    private int mVisiPosition = -1 ;

    public SubCategoryAdapter( Context context , int visiPosition, ListView  listView ){
        super(context) ;
        this.mContext = context.getApplicationContext()  ;
        this.mInflater = LayoutInflater.from(mContext) ;
        this.mVisiPosition = visiPosition ;
        this.mListView = listView ;
    }
    public void setVisiPosition(int visiPosition){
        this.mVisiPosition = visiPosition ;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if( convertView == null ){
            convertView = mInflater.inflate(R.layout.v2_subcategory_listitem, null) ;
            NoScrollGridView gridView = (NoScrollGridView) convertView.findViewById(R.id.subCategory_groupView_gridView) ;
            gridView.setNumColumns(5) ;

            final BaseListAdapter<IndustryNavigationData> listAdapter = new SubCategoryGridViewAdapter(mContext) ;
            gridView.setAdapter(listAdapter) ;
            gridView.setTag(listAdapter) ;

            ViewHold viewHold = new ViewHold() ;
            viewHold.gridLayout = (LinearLayout) convertView.findViewById(R.id.subCategory_groupView_gridViewLayout) ;
            viewHold.imageView = (ImageView_) convertView.findViewById(R.id.subCategory_groupView_imageview) ;
            viewHold.name = (TextView) convertView.findViewById(R.id.subCategory_groupView_name) ;
            viewHold.childsName = (TextView) convertView.findViewById(R.id.subCategory_groupView_childs) ;
            viewHold.gridView = gridView ;
            // TO search
            viewHold.gridView.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                    UILogUtils.saveAndAppendLogType(LogTypeCode.APP_DRESSCATEGORY_BROWSER_TWO);
                    MyOfferRequest request = new MyOfferRequest();
                    IndustryNavigationData item = listAdapter.getItem(position);
                    if (IndustryNavigationDataType.LEFT_CATEGORY.equals(item
                                                                        .getType())) {
                        request.setCategoryId(item.getId());
                        request.setCategoryName(item.getName());
                    } else if (IndustryNavigationDataType.LEFT_KEYWORD
                               .equals(item.getType())) {
                        request.setCategoryId(item.getId());
                        request.setKeywords(item.getName());
                    }
                    ActivityUtil.gotoSearchOfferList(request, mContext, true);
                }
            });

            viewHold.groupLayout = convertView.findViewById(R.id.subCategory_groupLayout) ;
            viewHold.groupLayout.setOnClickListener(clickListener) ;
            convertView.setTag(viewHold) ;
        }

        ViewHold viewHold = (ViewHold) convertView.getTag() ;
        viewHold.position = position ;
        IndustryNavigationData item = group.get(position) ;
        viewHold.groupLayout.setTag(viewHold) ;

        String url = item.getImageUrl() ;
        if( url != null ){
            viewHold.imageView.setImageResource(item.getImageUrl(), 110, 110, R.drawable.default_image) ;
        }

        viewHold.name.setText( item.getName() );
        StringBuffer sb = new StringBuffer() ;
        List<IndustryNavigationData> childs = item.getChildren() ;
        if( (childs!=null) && (childs.size()!=0) ){
            for( IndustryNavigationData itemChild : item.getChildren() ){
                sb.append(itemChild.getName()).append("/") ;
            }
            sb.deleteCharAt(sb.length()-1) ;
            viewHold.childsName.setText(sb.toString()) ;
        }else{
            viewHold.childsName.setText("") ;
        }

        BaseListAdapter<IndustryNavigationData> gridViewAdapter = (BaseListAdapter<IndustryNavigationData>) viewHold.gridView.getTag() ;
        gridViewAdapter.setList(item.getChildren()) ;
        if( (this.mVisiPosition==position) && (item.getChildren()!=null) && (item.getChildren().size()!=0) ){
            scrollView(viewHold, item, ExpandCollapseAnimation.EXPAND);
            viewHold.gridLayout.setVisibility(View.VISIBLE) ;
        }else{
            if( position == this.mPreVisiPosition ){
                //    viewHold.gridLayout.setVisibility(View.GONE) ;
                mPreVisiPosition = -1 ;
                scrollView(viewHold, item, ExpandCollapseAnimation.COLLAPSE);
            }else{
                viewHold.gridLayout.setVisibility(View.GONE) ;
            }
        }

        return convertView;
    }
    private class ViewHold{
        public int position ;
        public View groupLayout ;
        public LinearLayout gridLayout ;
        public ImageView_ imageView ;
        public TextView name ;
        public TextView childsName ;
        public NoScrollGridView gridView ;
    }

    OnClickListener clickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            UILogUtils.saveAndAppendLogType(LogTypeCode.APP_DRESSCATEGORY_BROWSER_ONE);
            ViewHold hold = (ViewHold) v.getTag() ;
            if( (mVisiPosition!=hold.position) ||  (hold.gridLayout.getVisibility()!=View.VISIBLE) ){
                mPreVisiPosition = mVisiPosition ;
                mVisiPosition = hold.position ;
                notifyDataSetChanged() ;
                //    scrollView(hold, getItem(mVisiPosition), ExpandCollapseAnimation.EXPAND) ;
                //    mListView.setSelection(mVisiPosition-1) ;
            }else{
                mPreVisiPosition = mVisiPosition ;
                mVisiPosition = -1 ;
                notifyDataSetChanged() ;
                //    notifyDataSetChanged() ;
            }
        }
    };

    /**
     * @param viewHold
     * @param item
     */
    private void scrollView(ViewHold viewHold, IndustryNavigationData item, int scrollType) {
        int childSize = item.getChildren().size() ;
        int row = childSize / 5 ;
        row = childSize%5==0? row:row+1 ;
        LayoutParams lp = viewHold.gridLayout.getLayoutParams() ;
        int heightDip = 37+(row-1)*13 ;
        int heightPx = DisplayUtil.dipToPixel(heightDip)+row*98 ;
        int time = heightPx*2 ;
        lp.height = heightPx ;
        viewHold.gridLayout.setLayoutParams(lp) ;
        viewHold.gridLayout.startAnimation(new ExpandCollapseAnimation(viewHold.gridLayout, time, scrollType, mListView, viewHold.position));
    }

}
4

0 回答 0