Android - 将转换应用于动态创建的视图

时间:2017-06-09 11:31:44

标签: android android-layout android-transitions

在我的Android活动中,我在顶部有一个TextView,它在整个和下面保持一个RelativeLayout,它是我的动态视图的容器。在线性布局中,我添加了一个具有多个TextView的LinearLayoutCompat对象。单击任何TextView时,将删除整个LinearLayoutCompat对象,并添加一个带有一组新TextView的新对象。现在我想添加过渡到LinearLayoutCompat视图的添加(从左侧滑入)和删除(向下滑出)。

我已经尝试创建自己的LinearLayoutCompat类名为LinearLay并覆盖onAttatchedToWindow()方法来设置转换(已被注释掉)但是它并没有显示任何转换(我最初只测试了添加视图)。

我的活动

    public class VegOptions extends AppCompatActivity{

        protected DBSource vegDBSource;
        private Cursor cursor;
        LayoutInflater li ;
        View v ;
        ViewGroup dataConPt ;
        Slide mslide;

        @Override
        public void onCreate(Bundle bundle) {
            super.onCreate(bundle);
            setContentView(R.layout.veg_option);
            vegDBSource = new DBSource(VegOptions.this);
            li = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = li.inflate(R.layout.veg_option, null);
            dataConPt = (ViewGroup) findViewById(R.id.dataCont);
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mslide = new Slide(Gravity.BOTTOM);
        }
            disp("VEGCAT");
        }

        private void disp(String COL)
        {   vegDBSource.open();
            cursor = vegDBSource.getInfo(COL);
            LinearLayoutCompat linearLayoutCompat = defaultPage(cursor, COL);
            if(linearLayoutCompat != null)
            {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                TransitionManager.beginDelayedTransition(dataConPt, mslide);
            }
                dataConPt.removeAllViews();
                dataConPt.addView(linearLayoutCompat);
            }
        }

        public LinearLayoutCompat defaultPage(Cursor mCursor, String COL) {
            if(mCursor==null || mCursor.getCount() == 0)
            {
                Toast.makeText(getApplicationContext(), "You're in a mood for " + COL + " !", Toast.LENGTH_SHORT).show();
                return null;
            }
            else {
                LinearLayoutCompat layout = new LinearLayoutCompat(this);
                LinearLayoutCompat.LayoutParams params = new LinearLayoutCompat.LayoutParams(LinearLayoutCompat.LayoutParams.MATCH_PARENT, LinearLayoutCompat.LayoutParams.MATCH_PARENT);
                layout.setLayoutParams(params);
                layout.setGravity(Gravity.CENTER);
                layout.setOrientation(LinearLayoutCompat.VERTICAL);
                mCursor.moveToFirst();
                do {
                    final AppCompatTextView defaultText = new AppCompatTextView(this);
                    defaultText.setText(mCursor.getString(mCursor.getColumnIndex(DataBaseHelper.COLUMN_TYPES)));
                    defaultText.setTextAppearance(getApplicationContext(), R.style.TextFontMain);
                    defaultText.setTextSize(30);
                    defaultText.setPadding(0, 30, 0, 30);
                    defaultText.setGravity(Gravity.CENTER);
                    defaultText.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View arg0) {
                            String choice = defaultText.getText().toString();
                            disp(choice);

                        }
                    });
                    layout.addView(defaultText);
                    mCursor.moveToNext();
                } while (mCursor.isAfterLast() == false);
                return layout;
            }
        }

        @Override
        protected void onResume() {
            super.onResume();
        }

        @Override
        protected void onPause() {
            super.onPause();
            vegDBSource.close();
            overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
        }
    }

通过创建Slide类(android.transition)的对象并使用TransitionManager.beginDelayedTransition(dataContPt, mSlide)自动将幻灯片转换应用于视图中的更改,我能够将转换应用于删除和添加视图。现在的问题是,在删除现有视图和添加新视图时,存在重叠。我希望首先删除现有视图,然后添加新视图。有什么方法可以引入这种延迟吗?

0 个答案:

没有答案