Как вы инициализируете view элементы?

Рейтинг: 1Ответов: 1Опубликовано: 18.02.2015

Здравствуйте!

В работе начал сталкиваться с тем, что на экране очень много разные вьюшек, сложная разметка и т.д. Что-то нужно показывать, что-то прятать и всегда помнить, какая переменная за что отвечает, чтобы по несколько раз не инициализировать. (Использую библиотеку AQuery вместо findViewById().) В итоге у меня выходит вот такой код. Правильно так делать? Как вы выходите из таких ситуаций?

 private void initAllViews() {
        emptyList = aq.id(R.id.tv_no_operations_text).getTextView();
        statementText = aq.id(R.id.tv_statment_text).getTextView();
        statementText.setTypeface(typeface);
        btnGetStatement = aq.id(R.id.bt_get_card_receipt).getButton();
        btnGetStatement.setTypeface(typeface);
        historyPeriod = aq.id(R.id.bankproductHistoryPeriod).getTextView();
        bankProductHistoryWeekly = aq.id(R.id.bankproductHistoryWeekly).getTextView();
        bankProductHistoryMonth = aq.id(R.id.bankproductHistoryMonth).getTextView();
        dateFrom = aq.id(R.id.bankproductDetailsDateFrom).getTextView();
        dateTill = aq.id(R.id.bankproductDetailsDateTill).getTextView();
        loadingCard = (LinearLayout) aq.id(R.id.ll_loading_card).getView();
        datePicker = (LinearLayout) aq.id(R.id.bankproductDetailsWeekPickerFrame).getView();
        transferToLayout = (LinearLayout) aq.id(R.id.ll_action_on_card).getView();
        transferFromLayout = (LinearLayout) aq.id(R.id.ll_action_from_card).getView();
        blockLayout = (LinearLayout) aq.id(R.id.ll_action_block_card).getView();
        payFromLayout = (LinearLayout) aq.id(R.id.ll_action_pay_from_card).getView();
        infoFrame = (LinearLayout) aq.id(R.id.bankproduct_details_infoFrame).getView();
        llRequisites = (LinearLayout) aq.id(R.id.ll_requisites).getView();
        actionsFrame = (LinearLayout) aq.id(R.id.bankproduct_details_actionsFrame).getView();
        cardBlocked = (LinearLayout) aq.id(R.id.bank_product_details_card_blocked).getView();
        creditInformation = (LinearLayout) aq.id(R.id.ll_credit_information).getView();
        bankProductInfo = (FrameLayout) aq.id(R.id.bankproduct_info_button_acc).getView();
        bankProductActions = (FrameLayout) aq.id(R.id.bankproduct_actions_button).getView();
        monthPickerFrame = (FrameLayout) aq.id(R.id.bankproductDetailsMonthPickerFrame).getView();
        frameCards = (FrameLayout) aq.id(R.id.frame_cards).getView();
        frameRefreshCards = (FrameLayout) aq.id(R.id.frame_refresh_cards).getView();
        frameProductDetails = (FrameLayout) aq.id(R.id.frame_product_details).getView();
        actionsText = aq.id(R.id.bankproduct_actions_text_acc).getTextView();
        infoText = aq.id(R.id.bankproduct_info_text_acc).getTextView();
        actionsSelected = aq.id(R.id.bankproduct_actions_selected).getView();
        infoSelected = aq.id(R.id.bankproduct_info_selected_acc).getView();
        progressBar = aq.id(R.id.progress_cards_receipt).getProgressBar();
        progressBarCards = aq.id(R.id.progress_cards).getProgressBar();
        refreshCardStatement = aq.id(R.id.iv_refresh_card_receipt).getImageView();
        errorText = aq.id(R.id.tv_error_text).getTextView();
    }

Ответы

▲ 1Принят

Использую AndroidAnnotations, примерно так:

@EActivity(R.layout.conversation_layout)
@OptionsMenu(R.menu.conversation_menu)
public class MyActivity extends SherlockFragmentActivity {

   @ViewById
   ListView conversationList;

   @ViewById
   EditText editMessage;

   @ViewById
   ImageButton sendMessage;

   @ViewById
   InviteConversationButton inviteConversationButton;

   @ViewById
   View buttonsLayout;

   @Pref
   MySettings settings;

   @App
   Me me;
   //ну и т.д.
}

Возникающие кастомные переменные показывающие различные состояния вьюшек и проч. UI элементы пихаю в кастомные ViewHolder типа:

public final class MessageViewHolder {
   //selector
   public boolean isThreadView;
   //fields
   public String messageId;
   public String conversationId;
   public String recipientIds;
   public ArrayList<String> addresses;
   //views
   public TextView respondent;
   public TextView body;
   public TextView date;
   public ImageView icon;
   public TextView unreadMessages;

   public MessageViewHolder() {
       addresses=new ArrayList<>();
       isThreadView=true;
   }
}

Далее по ходу пьесы пихаю их в именованные теги вьюшек:

view.setTag(R.layout.my_layout, messageViewHolder);

Очень удобно. Когда необходимо вытаскиваю из вьюшки и делаю то что нужно:

MessageViewHolder messageViewHolder=(MessageViewHolder)view.getTag(R.layout.my_layout);
if(messageViewHolder.isThreadView) {
     //blah-blah
}