2016-08-03 7 views
0

私はRecyclerViewを使用しています。そのAdapterには、各リストアイテムの画像とテキストがCardViewにあります。イメージを変更するすべてのカードのパラメータを表示する

質問:ImageViewの重みをプログラム的に変更したいと思います。

これは、画面の回転で実行されるコードです:

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 

    // Checks the orientation of the screen 
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { 
     SquareImageView imgView = (SquareImageView) findViewById(R.id.thumbnail); 
     LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) imgView.getLayoutParams(); 
     lp.weight = 0.1f; 
    } 
} 

コードが実行されますが、ただ一枚のカードにしていないすべてのカードの上に。

編集: 私アダプターコードが必要な場合:

// Provide a suitable constructor (depends on the kind of dataset) 
public MyAdapter(Context context, RecyclerView recyclerView, LinkedList<CardData> myDataset) 
{ 
    mContext = context; 
    mRecyclerView = recyclerView; 
    mDataset = myDataset; 
} 

// Create new views (invoked by the layout manager) 
@Override 
public ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) 
{ 
    // create a new view 
    final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_card_view, parent, false); 
    final ViewHolder viewHolder = new ViewHolder(view); 

    view.setOnTouchListener(new View.OnTouchListener() { 
     private GestureDetector gestureDetector = new GestureDetector(parent.getContext(), new GestureDetector.SimpleOnGestureListener() { 
      Not Relevant 
     }); 

     @Override 
     public boolean onTouch(View v, MotionEvent event) 
     { 
      return gestureDetector.onTouchEvent(event); 
     }}); 

    return viewHolder; 
} 

// Replace the contents of a view (invoked by the layout manager) 
@Override 
public void onBindViewHolder(ViewHolder holder, int position) 
{ 
    CardData currentCard = mDataset.get(position); 

    holder.mHeadline.setText(currentCard.mHeadline); 

} 

答えて

0

これはあなたのアダプターを変更する必要がonConfigurationChanged

であなたのRecyclerViewでリスト項目内のレイアウト項目を変更する実際の方法ではありませんコードとcheck if the orientation is tablet or not

だからあなたのアダプタの内側に、あなたのonBindViewHolder

if(tablet){ 
    lp.weight = 0.1f; 
} else { 
    lp.weight = 1; // Whatever 
} 

にこのようなものでレイアウト重みを設定する必要は今onConfigurationChangedにあなたは自分のアダプタをリセットする必要があります。

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 
    // Reset your adapter here. 
    myAdapter.resetAdapter(); 
} 
+0

正確にアダプタにはどこですか? OnBindViewHolderまたはOnCreateViewHolder?ありがとう! – Rani

+0

更新された回答をご覧ください。 –

+0

私はそれを読んだが、アダプタに関連するものは何も見つかりませんでした。さらに、そのような関数resetAdapterはありません。 – Rani

関連する問題