2

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

例如,我在 sqlite 数据库中搜索并获取 200 条记录,我想在我的活动中创建 200 个视图,但它会在 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();


       }


    }

请帮助我如何优化我的代码谢谢。

4

1 回答 1

2

好吧,android中有一个视图用于此特定目的,例如ListViewRecyclerView更多,这些视图获取Adapter对象,适配器恢复数据(在您的情况下为200行),而ListView For Example仅创建出现在屏幕上的项目, 当用户向上滚动时,ListView创建出现的新视图并删除旧视图,这给用户在巨大的项目列表中的奇妙用途,我建议学习使用ListViewAdapter如果你必须使用自己的自定义视图您可以ListView以自己的方式扩展和实施。

你可以在这里阅读更多

如果您想要不同的特定 gui,ListView或者您需要帮助,请告诉我

更新:如果您想创建自己的 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();

}

在您的下一页按钮或上一个

button.setOnClickListener(new View.OnClickListener {

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

})
于 2017-02-16T14:27:46.820 回答