刷新按钮不起作用

时间:2018-05-07 10:11:36

标签: java android

我的刷新Button无效Android Studio。这是我的主要活动代码(BreedList)。当我点击按钮时,列表应该刷新。请帮我解决一下这个。  我使用update breed方法刷新它。但它没有用。我创建了menu_main并将id作为刷新。并据此打电话。

public class BreedListActivity extends AppCompatActivity implements 
DownloadCallback {


private boolean mTwoPane;
private SimpleItemRecyclerViewAdapter adapter;
private int scrollPosition = 0;

private final static String SCROLL_POSITION = "ScrollPos";

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

    setUpImageLoader();

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    toolbar.setTitle(getTitle());

    if (findViewById(R.id.breed_detail_container) != null) {
       mTwoPane = true;
    }

    DogContentFragment.getInstance(getSupportFragmentManager());

    if (savedInstanceState != null) {
        scrollPosition = savedInstanceState.getInt(SCROLL_POSITION);
    }

    RecyclerView recyclerView = findViewById(R.id.breed_list);
    assert recyclerView != null;
    setupRecyclerView(recyclerView);
}

@Override
public boolean onCreateOptionsMenu (Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);
    return super.onCreateOptionsMenu(menu);
}
/*
 * Listen for option item selections so that we receive a notification
 * when the user requests a refresh by selecting the refresh action bar item.
 */
@Override
public boolean onOptionsItemSelected(MenuItem item) {


    if(item.getItemId() == R.id.refresh){

        updateBreeds();

        return true;
    }


    return super.onOptionsItemSelected(item);

}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt(SCROLL_POSITION, scrollPosition);
}

@Override
public void updateBreeds() {
    if (adapter != null) {
        adapter.notifyDataSetChanged();
    }
}

@Override
public void updateDogItem_URL(int position) {
    if (adapter != null) {
        adapter.notifyItemChanged(position, DogContentFragment.ITEMS.get(position).url);
    }
}

private void setUpImageLoader() {
    if (!ImageLoader.getInstance().isInited()) {
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
                .memoryCacheSize(25000000)
                .build();
        ImageLoader.getInstance().init(config);
    }
}

private void setupRecyclerView(@NonNull RecyclerView recyclerView) {
    this.adapter = new SimpleItemRecyclerViewAdapter(this, DogContentFragment.ITEMS, mTwoPane);
    recyclerView.setAdapter(adapter);

    recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                scrollPosition =
                    ((LinearLayoutManager) recyclerView.getLayoutManager()).
                            findFirstCompletelyVisibleItemPosition();
            }
            super.onScrollStateChanged(recyclerView, newState);
        }
    });

    recyclerView.getLayoutManager().scrollToPosition(scrollPosition);
}



public static class SimpleItemRecyclerViewAdapter
        extends RecyclerView.Adapter<SimpleItemRecyclerViewAdapter.ViewHolder> {

    private final AppCompatActivity mParentActivity;
    private final List<DogContentFragment.DogItem> mValues;
    private final boolean mTwoPane;


    private final View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            DogContentFragment.DogItem item = (DogContentFragment.DogItem) view.getTag();


            if (mTwoPane) {
                Bundle arguments = new Bundle();
                arguments.putString(BreedDetailFragment.ARG_ITEM_ID, item.id);
                BreedDetailFragment fragment = new BreedDetailFragment();
                fragment.setArguments(arguments);
                mParentActivity.getSupportFragmentManager().beginTransaction()
                        .replace(R.id.breed_detail_container, fragment, BreedDetailFragment.BREED_DETAIL_FRAGMENT)
                        .commit();
            } else {
                Context context = view.getContext();
                Intent intent = new Intent(context, BreedDetailActivity.class);
                intent.putExtra(BreedDetailFragment.ARG_ITEM_ID, item.id);

                context.startActivity(intent);
            }
        }
    };


    SimpleItemRecyclerViewAdapter(AppCompatActivity parent,
                                  List<DogContentFragment.DogItem> items,
                                  boolean twoPane) {
        mValues = items;
        mParentActivity = parent;
        mTwoPane = twoPane;
    }

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

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.mIdView.setText(mValues.get(position).title);

        String url = mValues.get(position).url;
        updateThumbnail(holder, url);

        holder.itemView.setTag(mValues.get(position));
        holder.itemView.setOnClickListener(mOnClickListener);
       }

    private void updateThumbnail(ViewHolder holder, String url) {
        if (url != null) {
            ImageLoader imageLoader = ImageLoader.getInstance();
            List<Bitmap> bitmaps = MemoryCacheUtils.findCachedBitmapsForImageUri(url, imageLoader.getMemoryCache());
            Bitmap loadedImage = !bitmaps.isEmpty() ? bitmaps.get(0) : null;

            if (loadedImage != null) {
                holder.mContentView.setImageBitmap(loadedImage);
            } else {
                DisplayImageOptions thumbOptions = new DisplayImageOptions.Builder()
                        .cacheInMemory(true)
                        .imageScaleType(ImageScaleType.EXACTLY)
                        .bitmapConfig(Bitmap.Config.RGB_565)
                        .build();

                imageLoader.displayImage(
                        url,
                        new ImageViewAware(holder.mContentView, false),
                        thumbOptions);
            }
        }
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position, List<Object> payloads) {
        if (payloads.isEmpty()) {
            super.onBindViewHolder(holder, position, payloads);
        } else {

            Object payload = payloads.get(0);

            if (payload instanceof String) {
                updateThumbnail(holder, (String) payload);
            }
        }
    }

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

    class ViewHolder extends RecyclerView.ViewHolder {
        final TextView mIdView;
        final ImageView mContentView;

        ViewHolder(View view) {
            super(view);
            mIdView = view.findViewById(R.id.item_name);
            mContentView = view.findViewById(R.id.item_thumb);
        }
    }
}
}

1 个答案:

答案 0 :(得分:0)

使用adapter.notifyDataSetChanged();将告诉适配器的观察者正在调整的内容已经改变。

另一方面,您需要使用yourListViewName.invalidateViews()来告诉ListView使其所有子项视图无效(重绘它们)。

因此,在您提交adapter.notifyDataSetChanged();后,请输入yourListViewName.invalidateViews()以重新加载ListView

希望有所帮助:)