Skip to content

Commit

Permalink
Merge pull request #1829 from aNaN9811/master
Browse files Browse the repository at this point in the history
#8 #251 修改MainActivity
  • Loading branch information
zengsn authored May 11, 2019
2 parents 048baeb + f83095e commit 471573f
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion students/soft1714080902206/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package com.exampler.mynews;

import android.Manifest;
import android.content.pm.PackageManager;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.PopupMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.exampler.mynews.databinding.ActivityMainBinding;

Expand All @@ -29,7 +34,11 @@ public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
initNews();
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.INTERNET}, 1);
} else {
initNews();
}
adapter = new NewsAdapter(newsList);
binding.recycleView.setAdapter(adapter);
binding.recycleView.setLayoutManager(new LinearLayoutManager(this));
Expand All @@ -47,6 +56,20 @@ public void onItemLongOnClick(View view, int pos) {
});
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case 1:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
initNews();
} else {
Toast.makeText(this, "You denied the peimission", Toast.LENGTH_SHORT).show();
}
break;
default:
}
}

public void showPopMenu(View view, final int pos){
PopupMenu popupMenu = new PopupMenu(this,view);
popupMenu.getMenuInflater().inflate(R.menu.menu_item,popupMenu.getMenu());
Expand Down

0 comments on commit 471573f

Please sign in to comment.