我无法弄清楚当我点击按钮时为什么没有调用onClick方法。但是,相同的代码片段在其他活动中工作正常。任何想法,我错过了什么?
这是Java文件
public class PicMem extends BaseActivity {
private String TAG = PicMem.class.getSimpleName();
private static final String endpoint = "https://api.androidhive.info/json/glide.json";
private ArrayList<Image> images;
private ProgressBar progressBar;
private GalleryAdapter mAdapter;
private EmptyRecyclerView recyclerView;
private View emptyView;
private Button btnRetry;
private SwipeRefreshLayout mSwipeRefreshLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.dailyzaib.siddhashram.R.layout.activity_pic_mem);
setupToolbar();
checkInternetConnection();
recyclerView = (EmptyRecyclerView) findViewById(com.dailyzaib.siddhashram.R.id.recycler_view);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
images = new ArrayList<>();
mAdapter = new GalleryAdapter(getApplicationContext(), images);
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getApplicationContext(), 2);
recyclerView.setLayoutManager(mLayoutManager);
emptyView = findViewById(R.id.recycler_view_empty);
recyclerView.setEmptyView(emptyView);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
recyclerView.addOnItemTouchListener(new GalleryAdapter.RecyclerTouchListener(getApplicationContext(), recyclerView, new GalleryAdapter.ClickListener() {
@Override
public void onClick(View view, int position) {
Bundle bundle = new Bundle();
bundle.putSerializable("images", images);
bundle.putInt("position", position);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
SlideshowDialogFragment newFragment = SlideshowDialogFragment.newInstance();
newFragment.setArguments(bundle);
newFragment.show(transaction, "slideshow");
}
@Override
public void onLongClick(View view, int position) {
}
}));
btnRetry = (Button) findViewById(R.id.btnRetry);
btnRetry.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e(TAG, "Calling onClick");
checkInternetConnection();
}
});
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeContainer);
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
checkInternetConnection();
mSwipeRefreshLayout.setRefreshing(false);
}
}, 500);
}
});
}
private void setupToolbar() {
final ActionBar ab = getActionBarToolbar();
ab.setHomeAsUpIndicator(com.dailyzaib.siddhashram.R.drawable.ic_menu);
ab.setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.refresh_images, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
openDrawer();
return true;
case R.id.refresh_pid:
checkInternetConnection();
break;
case R.id.menu_prefs:
startActivity(new Intent(this, SettingsPrefActivity.class));
break;
}
return super.onOptionsItemSelected(item);
}
@Override
protected int getSelfNavDrawerItem() {
return com.dailyzaib.siddhashram.R.id.nav_memories;
}
@Override
public boolean providesActivityToolbar() {
return true;
}
private void fetchImages() {
if (progressBar != null) {
progressBar.setVisibility(View.VISIBLE);
}
JsonArrayRequest req = new JsonArrayRequest(endpoint,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
if (progressBar != null) {
progressBar.setVisibility(View.GONE);
}
images.clear();
for (int i = 0; i < response.length(); i++) {
try {
JSONObject object = response.getJSONObject(i);
Image image = new Image();
image.setName(object.getString("name"));
JSONObject url = object.getJSONObject("url");
image.setSmall(url.getString("small"));
image.setMedium(url.getString("medium"));
image.setLarge(url.getString("large"));
image.setTimestamp(object.getString("timestamp"));
images.add(image);
} catch (JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
}
}
mAdapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Error: " + error.getMessage());
if (progressBar != null) {
progressBar.setVisibility(View.GONE);
}
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(req);
}
//////// Check Internet Connection ////////////////////
private void checkInternetConnection(){
new InternetCheckAsyncTask(this, new InternetCheckAsyncTask.InternetConsumer() {
@Override
public void internetStatusResult(Boolean internet) {
showSnack(internet);
}
});
}
private void showSnack(boolean isConnected) {
if (!isConnected) {
emptyView.setVisibility(View.VISIBLE);
Snackbar snackbar = Snackbar.make(findViewById(R.id.drawer_layout), "No Active Internet Connection", Snackbar.LENGTH_LONG)
.setAction("RETRY", new View.OnClickListener() {
@Override
public void onClick(View view) {
checkInternetConnection();
}
});
View snackbarLayout = snackbar.getView();
// SnackBar Message Text color
TextView textView = (TextView) snackbarLayout.findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(Color.YELLOW);
// Changing Action Button Text Color
snackbar.setActionTextColor(Color.WHITE);
snackbar.show();
}else{
emptyView.setVisibility(View.INVISIBLE);
fetchImages();
}
}
//////// End Of Check Internet Connection ////////////////////
@Override
protected void onResume() {
super.onResume();
checkInternetConnection();
}
}
这里是上述活动的XML布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/theme_primary_dark"
android:orientation="vertical">
<include layout="@layout/include_toolbar" />
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<include layout="@layout/include_nointernet" />
<include layout="@layout/include_progress_layout" />
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipeContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.dailyzaib.siddhashram.extra.EmptyRecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
</LinearLayout>
<include layout="@layout/include_navigation" />
</android.support.v4.widget.DrawerLayout>
最后,没有被调用的按钮(btnRetry)的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/recycler_view_empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
app:srcCompat="@drawable/ic_depress" />
<TextView
android:id="@+id/tvNoConnection"
style="@style/TextAppearance.AppCompat.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView"
android:layout_centerHorizontal="true"
android:layout_margin="10dp"
android:text="@string/no_internet_connection"
android:textColor="@color/cardview_light_background" />
<TextView
android:id="@+id/tvNoConnectionDesc"
style="@style/TextAppearance.AppCompat.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvNoConnection"
android:layout_centerHorizontal="true"
android:layout_margin="10dp"
android:text="@string/no_internet_description"
android:textColor="@color/cardview_light_background" />
<Button
android:id="@+id/btnRetry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvNoConnectionDesc"
android:layout_centerHorizontal="true"
android:layout_margin="10dp"
android:background="@drawable/button_background"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:text="@string/no_internet_try_again"
android:textColor="@color/cardview_light_background" />
</RelativeLayout>
答案 0 :(得分:1)
尝试更改
的位置<include layout="@layout/include_nointernet" />
在 SwipeRefreshLayout 内,如下所示
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipeContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include layout="@layout/include_nointernet" />
<com.dailyzaib.siddhashram.extra.EmptyRecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
</android.support.v4.widget.SwipeRefreshLayout>
答案 1 :(得分:0)
更改您的layout_visibility="visible"
或删除此属性。跟着它 -
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/recycler_view_empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible">
</RelativeLayout>
让我知道会发生什么。