В панели навигации, когда я щелкаю один из списков, я попадаю во фрагмент, где есть recyclerview. Когда у меня было небольшое количество данных, щелкните один из списка. он сразу переводит меня к списку фрагментов, но когда у меня есть большой объем данных и я щелкаю по нему, панель навигации не перемещается, она приостанавливается и внезапно открывается и показывает данные. Я хотел бы знать, почему он останавливается и не двигается. и я хотел бы отображать загружаемое изображение, когда я щелкаю список панели навигации. Когда я работаю с firebase, я помещаю изображение загрузки предупреждения прямо перед addvalueevent, но sqlite, я не уверен, куда его поместить. Любая помощь будет принята с благодарностью.
package com.donghyouny.biblecard;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.Menu;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.navigation.NavigationView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.core.view.GravityCompat;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, FragmentCallback {
Toolbar toolbar;
CardFragment cardFargment;
private AppBarConfiguration mAppBarConfiguration;
@SuppressLint("SourceLockedOrientationActivity")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_main);
// mAuth = FirebaseAuth.getInstance();
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
contextOfApplication = getApplicationContext();
toolbar.setTitle("draw card");
DrawerLayout drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
drawer.openDrawer(GravityCompat.START);
cardFargment = new CardFragment();
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
getSupportFragmentManager().beginTransaction().add(R.id.container, cardFargment).commit();
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
Bundle bundle = getBundle(menuItem);
// onFragmentSelected(0, bundle);
int id = menuItem.getItemId();
if(id == R.id.menu1){
onFragmentSelected(0, bundle);
}else if(id == R.id.menu2){
onFragmentSelected(1, bundle);
}else if(id == R.id.menu3) {
onFragmentSelected(2, bundle);
}else if(id == R.id.menu4) {
onFragmentSelected(3, bundle);
}else if(id == R.id.menu5) {
onFragmentSelected(4, bundle);
}else if(id == R.id.menu6) {
onFragmentSelected(5, bundle);
}else if(id == R.id.menu7) {
onFragmentSelected(6, bundle);
}else if(id == R.id.menu8) {
onFragmentSelected(7, bundle);
}else if(id == R.id.menu9) {
onFragmentSelected(8, bundle);
}else if(id == R.id.menu10) {
onFragmentSelected(9, bundle);
}else if(id == R.id.menu11) {
onFragmentSelected(10, bundle);
}else if(id == R.id.menu12) {
onFragmentSelected(11, bundle);
}else if(id == R.id.menu13) {
onFragmentSelected(12, bundle);
}else if(id == R.id.menu14) {
onFragmentSelected(13, bundle);
}else if(id == R.id.menu15) {
onFragmentSelected(14, bundle);
}else if(id == R.id.menu16) {
onFragmentSelected(15, bundle);
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private Bundle getBundle(@NonNull MenuItem menuItem) {
Bundle bundle = new Bundle();
bundle.putString("value", menuItem.getTitle().toString());
return bundle;
}
@Override
public void onFragmentSelected(int position, Bundle bundle) {
String value = bundle.getString("value");
Fragment curFragment = null;
if(position == 0){
curFragment = new CardFragment();
toolbar.setTitle(value);
}else if(position>=1){
curFragment = new Fragment1(value);
toolbar.setTitle(value);
}
toolbar.setTitle(value);
getSupportFragmentManager().beginTransaction().replace(R.id.container, curFragment).commit();
}
// a static variable to get a reference of our application context
public static Context contextOfApplication;
public static Context getContextOfApplication()
{
return contextOfApplication;
}
}
package com.donghyouny.biblecard;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import static com.donghyouny.biblecard.SplashActivity.ROOT_DIR;
public class Fragment1 extends Fragment {
public static final String VERSE_DATA = "data";
public static final int REQUEST_CODE = 101;
private RecyclerView recyclerView;
private BibleAdapter adapter;
private LinearLayoutManager mLayoutManager; //for sorting
private ArrayList<Bible> bList = new ArrayList<>();
private Connection conn;
private Statement stmt;
private ResultSet rs, rs2;
private Bible bible;
private String value;
private InterstitialAd mInterstitialAd;
private AlertDialog a;
public Fragment1(String value) {
this.value = value;
}
public Fragment1(){
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_1, container, false);
mInterstitialAd = new InterstitialAd(getContext());
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
mInterstitialAd.loadAd(new AdRequest.Builder().build());
conn = Utiles.dbConnection();
/* String dbName = "bible.db";
Connection conn=null;
try {
DriverManager.registerDriver((Driver) Class.forName("org.sqldroid.SQLDroidDriver").newInstance());
String dbURL = "jdbc:sqldroid:" + ROOT_DIR + "/" + dbName;
Log.d("url", dbURL);
conn = DriverManager.getConnection(dbURL);
}catch (Exception e){
e.printStackTrace();
}*/
try {
a = Utiles.createLoadingDialog(getActivity(), "Loading..");
stmt = conn.createStatement();
rs = stmt.executeQuery("select Id,BibleType, Verse, Content, Num, CNum, CheckNum, Image, datetime(TimeStamp) as MyDate from bible where CNum=" + "(select Id from category where CategoryName='" + value + "') order by TimeStamp asc");
while (rs.next()) {
bible = new Bible();
bible.setId(Integer.valueOf(rs.getString("Id")));
Log.d("bibleType",rs.getString("BibleType"));
bible.setBibleType(rs.getString("BibleType"));
Log.d("Verse", rs.getString("Verse"));
bible.setVerse(rs.getString("Verse"));
Log.d("Content", rs.getString("Content"));
bible.setContent(rs.getString("Content"));
bible.setNum(Integer.valueOf(rs.getString("Num")));
bible.setCnum(Integer.valueOf(rs.getString("CNum")));
bible.setCheckNum(Integer.valueOf(rs.getString("CheckNum")));
bible.setImage(rs.getBytes("Image"));
Log.d("TimeStamp", String.valueOf(rs.getLong("MyDate")));
bible.setTimestamp(rs.getString("MyDate"));
Log.d("bible", bible.toString());
bList.add(bible);
}
} catch (SQLException e) {
e.printStackTrace();
}
Log.d("blist", bList.toString());
recyclerView = rootView.findViewById(R.id.recyclerView);
//recyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(mLayoutManager);
adapter = new BibleAdapter(bList, getContext());
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
a.dismiss();
adapter.setOnItemClickListener(new OnVerseItemClickListener() {
@Override
public void onItemClick(BibleAdapter.BibleViewHolder holder, View view, final int position) { //Toast.makeText(getContext(), "num"+adapter.getItemCount(), Toast.LENGTH_LONG).show();
if(mInterstitialAd.isLoaded()) {
// Step 1: Display the interstitial
mInterstitialAd.show();
// Step 2: Attach an AdListener
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
Intent intent = new Intent(getContext(), VerseActivity.class);
Log.d("bible33", bList.get(position).toString());
intent.putExtra("bible", bList.get(position));
intent.putExtra("category", value);
Log.d("bibleIntent", bible.toString());
startActivityForResult(intent, 1);
}
});
}else{
Intent intent = new Intent(getContext(), VerseActivity.class);
Log.d("bible33", bList.get(position).toString());
intent.putExtra("bible", bList.get(position));
intent.putExtra("category", value);
Log.d("bibleIntent", bible.toString());
startActivityForResult(intent, 1);
}
}
});
setHasOptionsMenu(true);
return rootView;
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1){
getActivity().recreate();
/*Intent intent = getActivity().getIntent();
getActivity().finish();
startActivity(intent);*/
}
}
@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
inflater.inflate(R.menu.actionbar_actions, menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.save_folder:
Intent intent = new Intent(getContext(), SaveActivity.class);
startActivityForResult(intent, REQUEST_CODE);
return true;
}
return super.onOptionsItemSelected(item);
}
}
public class Utiles {
public static AlertDialog createLoadingDialog(Context context, String text) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
View view = LayoutInflater.from(context).inflate(R.layout.loading, null);
TextView tx = view.findViewById(R.id.loading_text);
tx.setText(text);
builder.setView(view);
AlertDialog dialog = builder.create();
dialog.setCanceledOnTouchOutside(false);
dialog.setCancelable(false);
dialog.show();
return dialog;
}