我有两个Java类。第一个活动是MainActivity
,它是名为HomeScreen的NavigationView活动。在content_layout中,我添加了Sliding Images Layout
的逻辑,该逻辑在名为Slider
的新类中实现。
运行应用程序时,滑动图像的布局为空。我认为我必须“组合”这两个活动,因此,当我运行该应用程序时,Slider
也将使滑动图像出现并起作用。
这是我的代码逻辑-
主屏幕活动
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import com.daimajia.slider.library.SliderLayout;
import static com.pertanian.puslitbangbun.fixyou.Login.my_shared_preferences;
import static com.pertanian.puslitbangbun.fixyou.Login.session_status;
public class HomeScreen extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
SharedPreferences sharedpreferences;
Boolean session = false;
Slider slider;
public static final String TAG_ID = "id";
public static final String TAG_USERNAME = "username";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
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();
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
sharedpreferences = getSharedPreferences(my_shared_preferences, Context.MODE_PRIVATE);
session = sharedpreferences.getBoolean(session_status, false);
if (session) {
navigationView.getMenu().findItem(R.id.admin).setVisible(false);
}
else {
navigationView.getMenu().findItem(R.id.logout).setVisible(false);
}
}
@Override
public void onBackPressed() {
DrawerLayout drawer = findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setMessage("Anda yakin ingin keluar?");
builder.setPositiveButton("Ya", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//if user pressed "yes", then he is allowed to exit from application
moveTaskToBack(true);
finish();
}
});
builder.setNegativeButton("Tidak", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//if user select "No", just cancel this dialog and continue with app
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
item.getItemId();
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.artikel) {
Intent i = new Intent(getApplicationContext(),MainActivity.class);
startActivity(i);
} else if (id == R.id.puslitbangbun) {
Intent i = new Intent(getApplicationContext(), Pemberlitri.class);
startActivity(i);
} else if (id == R.id.balittro) {
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
} else if (id == R.id.balittri) {
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
} else if (id == R.id.balittas) {
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
} else if (id == R.id.balitpalma) {
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
} else if (id == R.id.tentang) {
Intent i = new Intent(getApplicationContext(),Tentang.class);
startActivity(i);
} else if (id == R.id.admin) {
Intent i = new Intent(getApplicationContext(),Login.class);
startActivity(i);
} else if (id == R.id.logout) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setMessage("Anda yakin ingin Logout?");
builder.setPositiveButton("Ya", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//if user pressed "yes", then he is allowed to exit from application
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putBoolean(Login.session_status, false);
editor.putString(TAG_ID, null);
editor.putString(TAG_USERNAME, null);
editor.apply();
startActivity(new Intent(HomeScreen.this, HomeScreen.class));
}
});
builder.setNegativeButton("Tidak", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//if user select "No", just cancel this dialog and continue with app
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
Slider.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;
import com.daimajia.slider.library.Animations.DescriptionAnimation;
import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.BaseSliderView;
import com.daimajia.slider.library.SliderTypes.TextSliderView;
import com.daimajia.slider.library.Tricks.ViewPagerEx;
import java.util.HashMap;
public class Slider extends AppCompatActivity
implements BaseSliderView.OnSliderClickListener,
ViewPagerEx.OnPageChangeListener
{
SliderLayout sliderLayout ;
HashMap<String, String> HashMapForURL ;
HashMap<String, Integer> HashMapForLocalRes ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_home_screen);
sliderLayout = findViewById(R.id.slider);
//Call this method if you want to add images from URL .
//AddImagesUrlOnline();
//Call this method to add images from local drawable folder .
AddImageUrlFormLocalRes();
//Call this method to stop automatic sliding.
//sliderLayout.stopAutoCycle();
for(String name : HashMapForLocalRes.keySet()){
TextSliderView textSliderView = new TextSliderView(Slider.this);
textSliderView
.description(name)
.image(HashMapForLocalRes.get(name))
.setScaleType(BaseSliderView.ScaleType.Fit)
.setOnSliderClickListener(this);
textSliderView.bundle(new Bundle());
textSliderView.getBundle()
.putString("extra",name);
sliderLayout.addSlider(textSliderView);
}
sliderLayout.setPresetTransformer(SliderLayout.Transformer.DepthPage);
sliderLayout.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
sliderLayout.setCustomAnimation(new DescriptionAnimation());
sliderLayout.setDuration(3000);
sliderLayout.startAutoCycle();
sliderLayout.addOnPageChangeListener(Slider.this);
}
//@Override
//protected void onStop() {
// sliderLayout.stopAutoCycle();
//super.onStop();
//}
@Override
public void onSliderClick(BaseSliderView slider) {
Toast.makeText(this,slider.getBundle().get("extra") + "", Toast.LENGTH_SHORT).show();
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
@Override
public void onPageSelected(int position) {
Log.d("Slider Demo", "Page Changed: " + position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
public void AddImagesUrlOnline(){
HashMapForURL = new HashMap<>();
HashMapForURL.put("CupCake", "http://androidblog.esy.es/images/cupcake-1.png");
HashMapForURL.put("Donut", "http://androidblog.esy.es/images/donut-2.png");
HashMapForURL.put("Eclair", "http://androidblog.esy.es/images/eclair-3.png");
HashMapForURL.put("Froyo", "http://androidblog.esy.es/images/froyo-4.png");
HashMapForURL.put("GingerBread", "http://androidblog.esy.es/images/gingerbread-5.png");
}
public void AddImageUrlFormLocalRes(){
HashMapForLocalRes = new HashMap<>();
HashMapForLocalRes.put("CupCake", R.drawable.bgall);
HashMapForLocalRes.put("Donut", R.drawable.bgadmin);
HashMapForLocalRes.put("Eclair", R.drawable.bgall);
HashMapForLocalRes.put("Froyo", R.drawable.bgadmin);
HashMapForLocalRes.put("GingerBread", R.drawable.bgall);
}
}
答案 0 :(得分:0)
我认为问题出在这行
startActivity(new Intent(HomeScreen.this, HomeScreen.class));
应该是
startActivity(new Intent(HomeScreen.this, Slider.class));