如何实现片段的搜索栏?

时间:2019-05-13 15:07:12

标签: android android-fragments android-seekbar

我想将Seekbar实现为Fragment。我可以将其实现为任何活动都没有问题,但是对于我的片段来说,它是行不通的。它应该显示0-360之间的数字。

我试图在碎片中实现seekbar侦听器,但是我无法使用findviewbyid。

第一部分是我的片段,第二部分是主要活动。

package com.example.holdingpattern;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.support.annotation.Nullable;

public class holdingfragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_holding, null);



---------------------------------------------------------------------

package com.example.holdingpattern;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.SeekBar;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
    TextView textView;
    SeekBar seekBar;
    int min = 0, max = 360, current = 360;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        BottomNavigationView navView = findViewById(R.id.nav_view);
        navView.setOnNavigationItemSelectedListener(this);

        loadFragment(new holdingfragment());






        textView = findViewById(R.id.textView4);
        seekBar = findViewById(R.id.seekBar2);
        seekBar.setMax(max - min);
        seekBar.setProgress(current - min);
        textView.setText("" + current);

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                current = progress + min;
                textView.setText("" + current);

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });










    }


    private boolean loadFragment (Fragment fragment){
        if(fragment !=null){

            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.fragment_container, fragment)
                    .commit();


            return true;
        }
        return false;

    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        Fragment fragment = null;

        switch (menuItem.getItemId()){

            case R.id.navigation_holding:
                fragment = new holdingfragment();
                break;

            case R.id.navigation_wind:
                fragment = new windfragment();
                break;

            case R.id.navigation_answer:
                fragment = new answerfragment();
                break;


        }
        return loadFragment(fragment);

    }
}



0 个答案:

没有答案