如何在android中创建大动态视图?

如何在android中创建大动态视图?,android,performance,android-layout,android-view,dynamic-view,Android,Performance,Android Layout,Android View,Dynamic View,我在多次创建和删除视图时遇到了一个大问题 例如,我在sqlite数据库中搜索并获取了200条记录,我想在我的活动中创建200view,但它发生在3或4秒钟内,对用户体验和性能不利。如何增加每次创建视图的时间 这些是我创建视图的代码 public class CreateView { boolean header_flag=false; boolean first_widget=false; LinearLayout header_layout; List<

我在多次创建和删除视图时遇到了一个大问题

例如,我在sqlite数据库中搜索并获取了200条记录,我想在我的活动中创建200view,但它发生在3或4秒钟内,对用户体验和性能不利。如何增加每次创建视图的时间

这些是我创建视图的代码

public class CreateView {

    boolean header_flag=false;
    boolean first_widget=false;
    LinearLayout header_layout;

    List<Words_taha> words_tahaList=new ArrayList<>();




    public   void createHeader(Context context, LinearLayout main_layout, Words_taha words){



        if(header_flag==false){
            header_layout  =new LinearLayout(context);
            header_layout=new LinearLayout(context);
            header_layout.setOrientation(LinearLayout.HORIZONTAL);
            header_layout.setBackgroundResource(R.mipmap.sure_template);
            header_layout.setId(words.getW_id());

            header_layout.setGravity(Gravity.CENTER);

            main_layout.addView(header_layout);



            header_flag=true;

            words_tahaList.add(words);

        }
        else {

            words_tahaList.add(words);

            Collections.reverse(words_tahaList);


            for(int i=0;words_tahaList.size()>i;i++){

                TextView textView=new TextView(context);
               textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                       LinearLayout.LayoutParams.WRAP_CONTENT));

                textView.setText(words_tahaList.get(i).getW_text()+" ");
                textView.setTag(words_tahaList.get(i).getW_id());
                Typeface typeface=Typeface.createFromAsset(context.getAssets(),"fonts/arabicNeirizi.ttf");
                textView.setTypeface(typeface);
                textView.setTextColor(Color.parseColor("#000000"));

                header_layout.addView(textView);
            }

            words_tahaList.clear();
            header_flag=false;

        }



    }


   public void createLabelForMainWordsInOneLine(Activity context, LinearLayout main_layout, List<Words_taha> words_tahaList, int count ){


        LinearLayout linearLayout=new LinearLayout(context);

        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
       linearLayout.setGravity(Gravity.CENTER);


       if(words_tahaList.size()>0) {


           main_layout.addView(linearLayout);
           Collections.reverse(words_tahaList);

           for (Words_taha w : words_tahaList) {

               DroidTextView textView = new DroidTextView(context);

               textView.setText(w.getW_text());
               textView.setTag(w.getW_id());
               textView.setTextColor(Color.parseColor("#000000"));


               if (w.getW_type() == 3) {

                   textView.setLayoutParams(new LinearLayout.LayoutParams(130, 130));
                   textView.setGravity(Gravity.CENTER);
                   textView.setBackgroundResource(R.mipmap.sore);
               } else {
                  textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                         LinearLayout.LayoutParams.WRAP_CONTENT));



               }


               linearLayout.addView(textView);


           }

           words_tahaList.clear();


       }


    }
公共类CreateView{
布尔头_标志=false;
布尔值first_widget=false;
线性布局标题_布局;
列出单词\u tahaList=new ArrayList();
public void createHeader(上下文上下文、线性布局主布局、单词){
如果(标题_标志==false){
标题\布局=新的线性布局(上下文);
标题\布局=新的线性布局(上下文);
收割台布局。设置方向(线性布局。水平);
header\u layout.setBackgroundResource(R.mipmap.sure\u模板);
header_layout.setId(words.getW_id());
收割台布局设置重力(重心);
主布局。添加视图(标题布局);
标题_标志=真;
添加(单词);
}
否则{
添加(单词);
收藏。反面(单词_tahaList);
for(int i=0;words_tahaList.size()>i;i++){
TextView TextView=新的TextView(上下文);
textView.setLayoutParams(新的LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_内容,
LinearLayout.LayoutParams.WRAP_内容);
textView.setText(words_tahaList.get(i.getW_text()+);
setTag(words_tahaList.get(i).getW_id());
Typeface-Typeface=Typeface.createFromAsset(context.getAssets(),“font/arabicNeirizi.ttf”);
textView.setTypeface(字体);
textView.setTextColor(Color.parseColor(#000000”);
标题\布局。添加视图(文本视图);
}
单词_tahaList.clear();
标题_标志=false;
}
}
public void createLabelForMainWordsInOneLine(活动上下文、LinearLayout主布局、列表单词列表、整数计数){
LinearLayout LinearLayout=新的LinearLayout(上下文);
linearLayout.setOrientation(linearLayout.HORIZONTAL);
线性布局。设置重力(重心);
如果(单词\u tahaList.size()>0){
主布局。添加视图(线性布局);
收藏。反面(单词_tahaList);
用于(单词:单词){
DroidTextView textView=新的DroidTextView(上下文);
setText(w.getW_text());
setTag(w.getW_id());
textView.setTextColor(Color.parseColor(#000000”);
if(w.getW_type()==3){
setLayoutParams(新的LinearLayout.LayoutParams(130130));
textView.setGravity(Gravity.CENTER);
setBackgroundResource(R.mipmap.sore);
}否则{
textView.setLayoutParams(新的LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_内容,
LinearLayout.LayoutParams.WRAP_内容);
}
linearLayout.addView(文本视图);
}
单词_tahaList.clear();
}
}
请帮助我如何优化我的代码
谢谢。

android中有一个视图专门用于此目的,如
ListView
RecyclerView
等等,这些视图获得
Adapter
对象,适配器还原数据(在您的情况下是200行)例如,ListView只创建屏幕上显示的项目,当用户上下滚动时,
ListView
创建显示的新视图并删除旧视图,这给用户在庞大的项目列表中提供了一个奇妙的用途,如果您必须使用自己的,我建议lern使用
ListView
Adapter
自定义视图您可以从
ListView
扩展并以自己的方式执行

如果您需要与
列表视图不同的特定gui,或者您需要帮助,请告诉我

更新: 如果你想创建自己的impl,你认为这个方向如何

public abstract class CustomBookView extends LinearLayout implements CustomBookListener {

private int pageIndex = 0;
private List<WordsTasa> wordsList;

public CustomBookView(Context context, int pageIndex, List<WordsTasa> wordsList) {
    super(context);
    this.pageIndex = pageIndex;
    this.wordsList = wordsList;
}

public abstract View createPage(WordsTasa wordsTasa);

@Override
public void goNextPage() {
    if(wordsList.size()>=pageIndex+1)
        return;

    this.removeAllViews();
    //add your animation
    this.addView(createPage(wordsList.get(++pageIndex)));
}

@Override
public void goPreviousPage() {
    if(0<pageIndex-1)
        return;

    this.removeAllViews();
    //add your animation
    this.addView(createPage(wordsList.get(--pageIndex)));
}

public int getPageIndex() {
    return pageIndex;
}

public void setPageIndex(int pageIndex) {
    this.pageIndex = pageIndex;
}

public List<WordsTasa> getWordsList() {
    return wordsList;
}

public void setWordsList(List<WordsTasa> wordsList) {
    this.wordsList = wordsList;
}

public CustomBookView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CustomBookView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}


public static class WordsTasa {
    private String words;

    public WordsTasa(String words) {
        this.words = words;
    }

    public String getWords() {
        return words;
    }

    public void setWords(String words) {
        this.words = words;
    }
}

}

 public interface CustomBookListener {

void goNextPage();
void goPreviousPage();

}

为什么不使用
Listview
RecyclerView
@atef Hares,因为我想创建图书阅读器,Listview或RecyclerView对mecheck没有用处我知道Listview和适配器,但在这种情况下没有用,因为我们想在书中切换,但我想知道如何缩短创建视图的时间。好主意,谢谢aleady有很好的视图,正如你所知道的,创建视图和恢复视图对android设备来说是一项艰巨的工作,对于200个视图,你能做的最好的实现就是你可以自己实现的适配器实现,但在此之前,我建议你使用
AdapterViewFlipper
他非常容易使用,速度很快,你可以添加一个交换动画我使用了viewflipper,但每次幻灯片都会改变它,现在我将测试你的解决方案mayan anger我检查你的链接,这不是我的需要,因为我们在每个页面中没有相同的布局,而且我们需要动态创建小部件,mayan anger我创建了这个类,我不知道如何在这个类中填充数据,因为我没有604个页面他的书和每个页面都有自己的动态视图,另一方面当用户更改每个页面时,如何创建新视图
button.setOnClickListener(new View.OnClickListener {

public void onClick(View v) {
    customBookListener.goNextPage();
}

})