2017-03-03 62 views
1

我正在實現一個應用程序,用戶可以在其中存儲借記卡,稍後他們可以通過輸入同一張卡的CVV編號來使用它。我已將RecyclerView用於用戶存儲的所有項目(借記卡)。一切工作正常,視圖渲染都很好,我使用LinearLayoutManager顯示水平滾動。讓ReedlerView中的EditText成爲焦點

現在我面臨的問題是每當我點擊它時嘗試進入任何卡的CVV,視圖會轉向存儲卡列表的最後一項,所以如果我有三個卡片存儲在我的列表中,我嘗試爲第一張卡片輸入CVV,視圖直接轉移到第三張卡片上,但焦點仍保留在第一張卡片EditText上。我不知道發生了什麼事情。我分享一些相同的代碼部分。

設置適配器和定義水平滾動: -

recyclerAdapter = new RecyclerStoredCardAdapter(mContext, storedCards); 
       LinearLayoutManager layoutManager 
         = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false); 
       storedCardListRecycler.setLayoutManager(layoutManager); 
       storedCardListRecycler.setVisibility(View.VISIBLE); 
       storedCardListRecycler.setAdapter(recyclerAdapter); 

共享與此屏幕截圖,它會得到明確。任何幫助將是可觀的。謝謝。

enter image description here

+1

EditText與Listview或RecyclerView無法正常工作。 –

+0

您必須創建一個輸入對話框,同時單擊列表項以獲取輸入CVV編號。 –

+0

@Chetan感謝您的建議,但是您確定我們無法直接在該Edittext視圖中輸入CVV嗎? –

回答

1

我已經這樣做使用ListViewRecyclerView

但是你可以用RecyclerView也做。

這是我使用的類演示。

SettingItemListViewAdapter.java


/** 
* Created by vishalchhodwani on 18/10/16. 
*/ 
public class SettingItemListViewAdapter extends BaseAdapter { 

    private final String TAG = "SettingItemListViewAdapter"; 

    Context context; 
    List<SettingListViewItem> settingItemList; 
    OnMyClickListeners onMyClickListeners; 

    MyDatabaseAdapter myDatabaseAdapter; 

    public SettingItemListViewAdapter(Context context, List<SettingListViewItem> settingItemList) { 
     this.context = context; 
     this.settingItemList = settingItemList; 
     myDatabaseAdapter = new MyDatabaseAdapter(context); 
    } 

    public void setMyClickListener(OnMyClickListeners onMyClickListeners) { 

     this.onMyClickListeners = onMyClickListeners; 
    } 

    @Override 
    public int getCount() { 
     return settingItemList.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return settingItemList.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 

     final ViewHolder holder; 

     if (convertView == null) { 
      holder = new ViewHolder(); 

      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(R.layout.setting_listview_item, parent, false); 
      holder.settingListViewForm = (RelativeLayout) convertView.findViewById(R.id.settingListViewItem_form1); 
      holder.vrijeTekst = (EditText) convertView.findViewById(R.id.settingListViewItem_ed_virje_row1); 
      holder.kenteken = (EditText) convertView.findViewById(R.id.settingListViewItem_ed_kenketen_row1); 

      holder.checkRow = (ImageView) convertView.findViewById(R.id.settingListViewItem_check_row1); 
      holder.deleteRow = (ImageView) convertView.findViewById(R.id.settingListViewItem_deleteRow); 

      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     holder.vrijeTekst.setText(settingItemList.get(position).getItemVrijeTekst()); 
     holder.kenteken.setText(settingItemList.get(position).getItemKenteken()); 


     boolean isSelected = settingItemList.get(position).isItemSelected(); 
     holder.checkRow.setImageResource(isSelected ? R.drawable.checked : R.drawable.uncheked); 


     holder.vrijeTekst.setTag(position); 
     holder.vrijeTekst.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
      public void onFocusChange(final View v, boolean hasFocus) { 
       try { 
        if (!hasFocus) { 
         if (settingItemList.size() > 0) { 
          int position = (int) v.getTag(); 
          EditText Caption = (EditText) v; 
          settingItemList.get(position).setItemVrijeTekst(Caption.getText().toString()); 
         } 

        } else { 
         EditText caption = (EditText) v; 
         caption.setCursorVisible(true); 

        } 
       } catch (Exception ex) { 
        ex.printStackTrace(); 
       } 


      } 
     }); 


     holder.kenteken.setTag(position); 
     holder.kenteken.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
      public void onFocusChange(final View v, boolean hasFocus) { 

       try { 
        if (!hasFocus) { 
         if (settingItemList.size() > 0) { 
          int position = (int) v.getTag(); 
          EditText Caption = (EditText) v; 
          settingItemList.get(position).setItemKenteken(Caption.getText().toString()); 
         } 
        } else { 
         EditText caption = (EditText) v; 
         caption.setCursorVisible(true); 
        } 
       } catch (Exception ex) { 
        ex.printStackTrace(); 
       } 
      } 
     }); 

     holder.checkRow.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       if (myDatabaseAdapter.isAvailableInTable(settingItemList.get(position).getItemId())) 
        onMyClickListeners.onSelectButtonClicked(position); 
      } 
     }); 

     holder.deleteRow.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       onMyClickListeners.onDeleteItemButtonClicked(position); 
      } 
     }); 


     if (getCount() == position + 1) { 
      holder.vrijeTekst.requestFocus(); 
      holder.vrijeTekst.performClick(); 
     } 

     return convertView; 
    } 

    public static class ViewHolder { 
     RelativeLayout settingListViewForm; 
     EditText vrijeTekst, kenteken; 
     ImageView checkRow, deleteRow; 
    } 
} 

SettingN_New.java(這是一個片段)


public class SettingN_New extends Fragment implements OnClickListener, OnMyClickListeners { 

    private final String TAG = "SettingN_New"; 

    Context context; 

    private TextView tv_demo; 
    ToggleButton togglebtn_save; 
    Button btn_save, btn_add; 

    ListView settingItemListView; 
    List<SettingListViewItem> settingItemList; 
    SettingItemListViewAdapter settingItemListViewAdapter; 

    MyDatabaseAdapter myDatabaseAdapter; 

    TinyDB loginpref; 

    boolean isNewRowAdded = true; 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 

     //This line of code will stay focus on selected edittext in list 
     getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST); 

     ActionBar bar = getActivity().getActionBar(); 
     bar.show(); 

     View rootView = inflater.inflate(R.layout.setting_new, container, false); 

     initializeViews(rootView); 

     setUI(); 

     getListOfItems(); 

     return rootView; 
    } 

    private void setUI() { 

     try { 
      if (loginpref.getBoolean(ConstantLib.PREF_AUTO_LOGIN)) { 
       togglebtn_save.setChecked(true); 
      } else { 
       togglebtn_save.setChecked(false); 
      } 
     } catch (Exception e) { 
      // TODO: handle exception 
      e.printStackTrace(); 
     } 
    } 

    private void initializeViews(View rootView) { 
     context = getActivity(); 

     LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View headerView = inflater.inflate(R.layout.setting_listview_header, null); 

     btn_save = (Button) headerView.findViewById(R.id.btn_save); 
     btn_add = (Button) headerView.findViewById(R.id.btn_add); 
     togglebtn_save = (ToggleButton) headerView.findViewById(R.id.togglebtn_save); 
     tv_demo = (TextView) headerView.findViewById(R.id.tv_demo); 

     settingItemList = new ArrayList<>(); 
     settingItemListView = (ListView) rootView.findViewById(R.id.setting_listView); 
     settingItemListView.setClickable(true); 
     settingItemListView.refreshDrawableState(); 
     settingItemListView.addHeaderView(headerView); 
     settingItemListView.setItemsCanFocus(true); 
     settingItemListViewAdapter = new SettingItemListViewAdapter(context, settingItemList); 
     settingItemListViewAdapter.setMyClickListener(this); 
     settingItemListView.setAdapter(settingItemListViewAdapter); 


     myDatabaseAdapter = new MyDatabaseAdapter(context); 
     loginpref = new TinyDB(getActivity()); 

     btn_save.setOnClickListener(this); 
     btn_add.setOnClickListener(this); 
     tv_demo.setOnClickListener(this); 

     togglebtn_save.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(CompoundButton buttonView, 
             boolean isChecked) { 
       // TODO Auto-generated method stub 

       loginpref.putBoolean(ConstantLib.PREF_AUTO_LOGIN, isChecked); 
      } 
     }); 
    } 


    private void getListOfItems() { 

     settingItemList.clear(); 
     settingItemList.addAll(myDatabaseAdapter.getAllData()); 

     if (settingItemList.size() == 0) { 
      isNewRowAdded = true; 
      SettingListViewItem settingListViewItem = new SettingListViewItem(); 
      settingListViewItem.setItemId(settingItemList.size() + ""); 
      settingListViewItem.setItemVrijeTekst(""); 
      settingListViewItem.setItemKenteken(""); 
      settingListViewItem.setItemSelected(false); 

      settingItemList.add(settingListViewItem); 
     } else { 
      isNewRowAdded = false; 
     } 
     notifyDataSetChanged(); 
    } 

    private void hideKeyboard() { 
     // Check if no view has focus: 
     View view = getActivity().getCurrentFocus(); 
     if (view != null) { 
      InputMethodManager inputManager = (InputMethodManager) getActivity() 
        .getSystemService(Context.INPUT_METHOD_SERVICE); 
      inputManager.hideSoftInputFromWindow(view.getWindowToken(), 
        InputMethodManager.HIDE_NOT_ALWAYS); 
     } 
    } 

    @Override 
    public void onClick(View v) { 
     switch (v.getId()) { 
      case R.id.btn_save: 

       clearFocus(); 

       hideKeyboard(); 

       new Handler().postDelayed(new Runnable() { 
        @Override 
        public void run() { 

         if (isValidationSuccess()) 
          saveAllData(); 
        } 
       }, 200); 
       break; 
      case R.id.btn_add: 
       addAnotherRow(); 
       break; 
      case R.id.tv_demo: 
       clickedOnTvDemo(); 
       break; 
     } 
    } 

    private void clickedOnTvDemo() { 

     try { 
      Intent i = new Intent(getActivity(), Setting_exp_activity.class); 
      startActivity(i); 

     } catch (Exception e) { 
      // TODO: handle exception 
      e.printStackTrace(); 
     } 
    } 

    private boolean isValidationSuccess() { 
     Log.e(TAG, "isValidationSuccess() called : settingItemList.size()==" + settingItemList.size()); 

     for (int i = 0; i < settingItemList.size(); i++) { 
      if (settingItemList.get(i).getItemVrijeTekst().equalsIgnoreCase("") || settingItemList.get(i).getItemKenteken().equalsIgnoreCase("")) { 
       showToast("Veld mag niet leeg zijn");// showToast("Fields should not be empty!!"); 
       return false; 
      } 
     } 

     return true; 
    } 

    private void saveAllData() { 
     Log.e(TAG, "saveAllData() called"); 

     myDatabaseAdapter.clearTable(); 

     for (int i = 0; i < settingItemList.size(); i++) { 
      isNewRowAdded = false; 
      SettingListViewItem settingListViewItem = new SettingListViewItem(); 
      settingListViewItem.setItemId(i + ""); 
      settingListViewItem.setItemVrijeTekst(settingItemList.get(i).getItemVrijeTekst()); 
      settingListViewItem.setItemKenteken(settingItemList.get(i).getItemKenteken()); 
      settingListViewItem.setItemSelected(settingItemList.get(i).isItemSelected()); 

      myDatabaseAdapter.insertDataToTable(settingListViewItem); 
     } 

     DialogUtils.showInfoDialog(getActivity(), 
       "Instellingen opgeslagen"); 
    } 

    private void addAnotherRow() { 
     Log.e(TAG, "addAnotherRow() called"); 
     if (settingItemList.size() > 0 && !isNewRowAdded) { 
      if (!settingItemList.get(settingItemList.size() - 1).getItemVrijeTekst().equalsIgnoreCase("") && !settingItemList.get(settingItemList.size() - 1).getItemKenteken().equalsIgnoreCase("")) { 
       isNewRowAdded = true; 
       Log.e(TAG, "addAnotherRow() called check 1"); 

       SettingListViewItem settingListViewItem = new SettingListViewItem(); 
       settingListViewItem.setItemId(settingItemList.size() + ""); 
       settingListViewItem.setItemVrijeTekst(""); 
       settingListViewItem.setItemKenteken(""); 
       settingListViewItem.setItemSelected(false); 
       settingItemList.add(settingListViewItem); 

       notifyDataSetChanged(); 
      } else { 
       Log.e(TAG, "addAnotherRow() called check 2"); 
       showToast("Al toegevoegd");// showToast("Already Added!!"); 
      } 
     } else { 
      if (!isNewRowAdded) { 
       isNewRowAdded = true; 
       Log.e(TAG, "addAnotherRow() called check 3"); 
       SettingListViewItem settingListViewItem = new SettingListViewItem(); 
       settingListViewItem.setItemId("0"); 
       settingListViewItem.setItemVrijeTekst(""); 
       settingListViewItem.setItemKenteken(""); 
       settingListViewItem.setItemSelected(false); 
       settingItemList.add(settingListViewItem); 

       notifyDataSetChanged(); 
      } else { 
       Log.e(TAG, "addAnotherRow() called check 4"); 
       showToast("Al toegevoegd");// showToast("Already Added!!"); 
      } 
     } 

     settingItemListView.setSelection(settingItemList.size()); 

     Log.e(TAG, "addAnotherRow() called check 5"); 
     Log.e(TAG, "after settingItemList.size()==" + settingItemList.size()); 
    } 

    @Override 
    public void onDeleteItemButtonClicked(int position) { 

     Log.e(TAG, "onDeleteItemButtonClicked() position==" + position); 

     if (myDatabaseAdapter.getAllData().size() > 0) 
      showAlertForDeleteItem(position); 
     else 
      showToast("Er is geen item te verwijderen"); // showToast("No item to Delete"); 
    } 

    @Override 
    public void onSelectButtonClicked(int position) { 
     Log.e(TAG, "onSelectButtonClicked() position==" + position); 

     for (int i = 0; i < settingItemList.size(); i++) { 
      Log.e(TAG, "onSelectButtonClicked() called check 3"); 
      settingItemList.get(i).setItemSelected(false); 
     } 

     settingItemList.get(position).setItemSelected(true); 

     notifyDataSetChanged(); 
    } 

    private void showAlertForDeleteItem(final int position) { 
     AlertDialog.Builder alertDialog = new AlertDialog.Builder(context); 
     alertDialog.setMessage("Weet je zeker dat je dit item wilt wissen?");//alertDialog.setMessage("Are you sure you want to delete this item?"); 

     // ja==yes 
     alertDialog.setPositiveButton("Ja", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 

       myDatabaseAdapter.deleteTableRow(settingItemList.get(position).getItemId() + ""); 
       settingItemList.remove(position); 
       settingItemListViewAdapter.notifyDataSetChanged(); 

       if (settingItemList.size() == position + 1) { 
        isNewRowAdded = false; 
       } 

       if (settingItemList.size() == 0) { 
        isNewRowAdded = false; 
        addAnotherRow(); 
       } 
      } 
     }); 

     //Annuleer==cancel 
     alertDialog.setNegativeButton("Annuleer", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 


      } 
     }); 

     alertDialog.show(); 
    } 

    private void notifyDataSetChanged() { 
     settingItemListViewAdapter.notifyDataSetChanged(); 
    } 

    private void showToast(String message) { 
     Toast.makeText(context, message, Toast.LENGTH_LONG).show(); 
    } 

    public void clearFocus() { 
     if (getActivity().getWindow().getCurrentFocus() != null) { 
      getActivity().getWindow().getCurrentFocus().clearFocus(); 
     } 
    } 
} 

我給你我的整個類。爲什麼?因爲它會讓你更加了解我是如何將它與ListView一起使用的。

測試它並讓我知道。 :)

  • 這行代碼是非常重要的 - getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST);
+0

你能分享你的適配器代碼嗎? – user320676

+0

@ user320676我有共享已命名爲'SettingItemListViewAdapter.java'。 –

+0

哦,無論如何,檢查此鏈接https://github.com/niravkalola/RecyclerView – user320676

0

無法重現這個問題。讓我知道如果下面的代碼不會做你正在面對的。如果不是,可以提供更多的反饋,如果你分享你的適配器的代碼。

MainActivity.java 
public class MainActivity 
    extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view); 
    MyAdapter adapter = new MyAdapter(); 
    recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)); 
    recyclerView.setAdapter(adapter); 
} 
} 


public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { 

@Override 
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_ite, parent); 
    return new MyViewHolder(view); 
} 

@Override 
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 
    ((EditText)((MyViewHolder)holder).mBinding.findViewById(R.id.editText)).setHint(position + " "); 
} 

@Override 
public int getItemCount() { 
    return 100; 
} 

public class MyViewHolder extends RecyclerView.ViewHolder { 
    View mBinding; 
    public MyViewHolder(View binding) { 
     super(binding); 
     this.mBinding = binding; 
    } 
} 
} 

the 2xml layouts. 
<android.support.v7.widget.CardView 
android:id="@+id/card_view" 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
xmlns:android="http://schemas.android.com/apk/res/android"> 

<EditText 
    android:id="@+id/editText" 
    android:layout_height="200dp" 
    android:layout_width="300dp"/> 

</android.support.v7.widget.CardView> 

我main_activity.xml如下:

<RelativeLayout 
android:layout_height="match_parent" 
android:layout_width="match_parent" 
xmlns:android="http://schemas.android.com/apk/res/android"> 

<android.support.v7.widget.RecyclerView 
    android:id="@+id/recycler_view" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent"/> 
</RelativeLayout> 

正如你可以看到這是最簡單的情況。讓我知道我是否錯過了任何東西。