Stacked Q&A

Sliding images doesn't work
  • 0 voting
  • 2022-08-12 00:00

    I have two Java Classes. The first activity is the MainActivity which is the NavigationView Activity named HomeScreen. In the content_layout I added Sliding Images Layout logic of which is implemented in a new class named Slider.

    When I run the app, the layout for the sliding images is empty. I think I have to "combine" these two activities so when I run the app, Slider will also work to make the sliding images appeared and working.

    Here is my code logic -

    HomeScreen Activity

    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);
    
        }
    }
    

  • 1 67 0 0
첨부 파일
답변
총 1개의 답변이있습니다.
  • 답변일: 2022-08-12 00:00

    i think the problem is in this line

    startActivity(new Intent(HomeScreen.this, HomeScreen.class));
    

    rather it should be

    startActivity(new Intent(HomeScreen.this, Slider.class));
    
  • 0 0 퍼블 채택!
Tages
    활동순위
    유저
      파트너사