2016-11-19 8 views
0

小さなアプリを作成して問題を表示します。 こちらをご覧ください。https://github.com/Anton111111/ExampleGlideBlinkGlide and RecyclerView点滅

メインクラス:https://github.com/Anton111111/ExampleGlideBlink/blob/master/app/src/main/java/com/example/testglide/MainActivity.java

public class MainActivity extends AppCompatActivity { 

    private Adapter adapter; 
    private Handler handler; 

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

     RecyclerView rv = (RecyclerView) findViewById(R.id.recycler_view); 
     rv.setHasFixedSize(true); 
     GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2); 
     rv.setLayoutManager(gridLayoutManager); 

     ArrayList<String> items = new ArrayList<>(Arrays.asList(new String[]{ 
       "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", 
       "11", "12", "13", "14", "15", "16", "17", "18", "19", "20" 
     })); 

     adapter = new Adapter(this, items); 
     rv.setAdapter(adapter); 
     handler = new Handler(); 
     handler.postDelayed(r, 500); 
    } 

    Runnable r = new Runnable() { 
     @Override 
     public void run() { 
      Random rand = new Random(System.currentTimeMillis()); 
      handler.postDelayed(r, 500); 
      adapter.notifyItemChanged(rand.nextInt(4)); 
     } 
    }; 

    public class Adapter extends RecyclerView.Adapter { 

     private final Context ctx; 
     private ArrayList<String> items = new ArrayList<>(); 
     private int progress = 1; 

     public Adapter(Context context, ArrayList<String> items) { 
      this.items = items; 
      ctx = context; 
     } 

     @Override 
     public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
      View v = LayoutInflater.from(parent.getContext()) 
        .inflate(R.layout.item, parent, false); 
      Holder h = new Holder(v); 
      return h; 
     } 

     @Override 
     public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 
      ((Holder) holder).getProgress().setText(progress+""); 
      progress++; 
      Glide.with(ctx) 
        .load(Uri.parse("file:///android_asset/" + (position + 1) + ".png")) 
        .into(((Holder) holder).getImage1()); 
     } 

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

     @Override 
     public int getItemCount() { 
      return items.size(); 
     } 

     public class Holder extends RecyclerView.ViewHolder { 

      private final ImageView image1; 
      private final TextView progress; 

      public ImageView getImage1() { 
       return image1; 
      } 

      public TextView getProgress() { 
       return progress; 
      } 

      public Holder(View itemView) { 
       super(itemView); 
       image1 = (ImageView) itemView.findViewById(R.id.image1_view); 
       progress = (TextView) itemView.findViewById(R.id.progress_text); 
      } 
     } 
    } 

} 

主レイアウト: https://github.com/Anton111111/ExampleGlideBlink/blob/master/app/src/main/res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.example.testglide.MainActivity"> 

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

アイテムのレイアウト: https://github.com/Anton111111/ExampleGlideBlink/blob/master/app/src/main/res/layout/item.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <ImageView 
     android:id="@+id/image1_view" 
     android:layout_width="100dp" 
     android:layout_height="100dp" /> 

    <TextView 
     android:id="@+id/progress_text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 
</LinearLayout> 

私はRecyclerViewアダプタにnotifyItemChangedを呼び出します私はテキスト(私の例では、ID R.id.progressのEditText)とイメージを変更せずに表示するだけです。しかし、それは点滅します。

私は点滅することなくテキストを変更できますか?

+0

https://stackoverflow.com/a/45014231/5848430のような正方形のレイアウトを使用して行レイアウトファイルを囲み – MakBeard

答えて

0

public class SquaredFrameLayout extends FrameLayout { 
public SquaredFrameLayout(Context context) { 
    super(context); 
} 

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

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

@TargetApi(Build.VERSION_CODES.LOLLIPOP) 
public SquaredFrameLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
    super(context, attrs, defStyleAttr, defStyleRes); 
} 

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    super.onMeasure(widthMeasureSpec, widthMeasureSpec); 
}} 

そして、あなたのxmlで私は答えにこの問題を解決し

<com.app_views.SquaredFrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ImageView 
     android:id="@+id/iv_answer_image" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:onClick="@{group.onHashTagPhotoClicked}" 
     android:paddingEnd="1dp" 
     android:paddingLeft="1dp" 
     android:paddingRight="1dp" 
     android:paddingStart="1dp" 
     android:paddingTop="1dp" 
     app:galleryImage="@{group.boothPhoto}" /> 
</com.app_views.SquaredFrameLayout>