标签: android-fragments

Android.我要用片段吗?

我无法理解片段的真实用途.我想创建一个笔记应用程序.是否需要使用碎片?如果一个应用程序使用Android 3.0下的片段用户不能使用该应用程序?

android android-fragments

-1
推荐指数
1
解决办法
42
查看次数

只显示一个片段

在我的应用程序中,我使用三个片段活动用于导航抽屉,但是在使用片段事务替换一个片段时总是显示最后一个片段为什么?这是我的代码: -

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Fragment fragment = new HomeFragment();
    Class fragmentClass = null;
    switch (position) {
        case 0:
            Log.d("position1", String.valueOf(position));
            fragmentClass = FarecardFragment.class;
        case 1:
            Log.d("position1", String.valueOf(position));
            fragmentClass = MybookingFragment.class;
        case 2:
            Log.d("position1", String.valueOf(position));
            fragmentClass = TrackingFragment.class;
    }
    try {
        fragment = (Fragment) fragmentClass.newInstance();
    } catch (Exception e) {
        e.printStackTrace();
    }
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.mainContent, fragment).commit();
}
Run Code Online (Sandbox Code Playgroud)

android android-fragments

-1
推荐指数
1
解决办法
58
查看次数

片段上的ImageView不可见

我有一个片段(实际上是一个SupportMapFragment),我想在其上的一个ImageView放置在地图的一角,但并不能覆盖所有的地图片段,只是一点点。我的布局:

<ImageView
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:id="@+id/imagenTick"
    android:background="@color/componente_boton_base_seleccionar_defecto"
    android:scaleType="fitCenter"
    android:adjustViewBounds="true"
    android:src="@drawable/tick"
    android:clickable="false"
    android:contentDescription="ImagenTick"
    android:layout_alignTop="@+id/mapfragment"
    android:layout_alignLeft="@+id/mapfragment"
    android:layout_alignStart="@+id/mapfragment"/>

<fragment
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:id="@+id/mapfragment" />
Run Code Online (Sandbox Code Playgroud)

现在,ImageView在mapFragment后面,并且它不可见。我希望它可见,但是找不到一种方法告诉Android Studio我希望它在地图上使其可见,而不是在后面。

有什么办法可以实现?

谢谢。

android imageview android-fragments

-1
推荐指数
1
解决办法
1031
查看次数

Android - 在Fragment中使用SharedPrefs

我正在尝试使用共享首选项.

我已经创建了一个类 -

package com.bscheme.linkkin.utils;

import android.content.Context;
import android.content.SharedPreferences;
import com.bscheme.linkkin.R;

public class SharedDataSaveLoad {

    public static void save(Context context, String key, String value) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(context.getResources().getString(R.string.preference_file_key),Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        editor.commit();
    }
    public static void save(Context context,String key, int value) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(context.getResources().getString(R.string.preference_file_key),Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt(key, value);
        editor.commit();
    }

    public static String  load(Context context, String key) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(context.getResources().getString(R.string.preference_file_key), Context.MODE_PRIVATE);
        return sharedPreferences.getString(key, "");
    }
    public static int loadInteger(Context context,String …
Run Code Online (Sandbox Code Playgroud)

android sharedpreferences android-fragments

-1
推荐指数
1
解决办法
325
查看次数

非静态方法getActivity()

我使方法"showResult"静态但我有问题:

"Toast.makeText(`getActivity()`.getApplication(), result2 + "\n" + "Total Amount:=" + totalAmount2 + "€", Toast.LENGTH_LONG).show();"
Run Code Online (Sandbox Code Playgroud)

怎么解决我的问题?我将分享我的代码.

感谢每一个人!

这是我的第一个类Fragment.java

public class MyListFragment extends Fragment implements
        android.widget.CompoundButton.OnCheckedChangeListener {

    ListView lv;
    ArrayList<Planet> planetList;
    static PlanetAdapter plAdapter;
    BirraAdapter biAdapter;
    PlanetAdapter.PlanetHolder holder;




    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_list2, container, false);

        Button mButton = (Button) rootView.findViewById(R.id.button);
        mButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showResult(v);


            }
        });
        //return inflater.inflate(R.layout.fragment_list2, container, false);
        return …
Run Code Online (Sandbox Code Playgroud)

android static-methods android-fragments android-studio

-1
推荐指数
1
解决办法
3525
查看次数

错误找不到合适的add方法(int,Fragment)

我试图在Activity中膨胀片段,这是代码.

public class DetailActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_detail);
            if (savedInstanceState == null) {
                getSupportFragmentManager().beginTransaction()
                        .add(R.id.container2, new DetailFragment()) 
                        .commit();    //Line with Error
            }
        }


        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.detail, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up …
Run Code Online (Sandbox Code Playgroud)

error-handling android exception-handling android-fragments

-1
推荐指数
1
解决办法
2159
查看次数

getActivity().getComponentName()into Fragment Nullpointerexception

在多选项卡应用程序中,我在其中一个选项卡中添加了一个可扩展的列表视图和一个搜索视图(Tab2).我在getComponentName()上有一个错误,因为它不支持fragment.So我添加了getActivity().getComponentName().在此行上运行它时没有错误但是NullpointerException.应用程序崩溃.有任何想法吗?
以下是代码的重要部分:

public class Tab2 extends Fragment implements SearchView.OnQueryTextListener,SearchView.OnCloseListener{
private SearchView search;
private MylistAdapter listAdapter;
private ExpandableListView myList;
private ArrayList<Continent> continentList = new ArrayList<Continent>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.tab2, container, false);
    SearchManager searchManager = (SearchManager)  getActivity().getSystemService(Context.SEARCH_SERVICE);
    search = (SearchView) getActivity().findViewById(R.id.search);
    search.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
    search.setIconifiedByDefault(false);
    search.setOnQueryTextListener(this);
    search.setOnCloseListener(this);

    displayList();
    expandAll();
    return rootView;
}



private void expandAll(){

    int count = listAdapter.getGroupCount();
    for (int i=0; i<count; i++)
    {
        myList.expandGroup(i);
    }
}

private void displayList() {

    // display …
Run Code Online (Sandbox Code Playgroud)

android nullpointerexception android-fragments searchview

-1
推荐指数
1
解决办法
2671
查看次数

如何从覆盖活动的片段中膨胀菜单

我有一个片段菜单,但它没有按预期显示。它可能被活动工具栏覆盖

如何从片段菜单中显示菜单?现在显示的是来自活动

片段菜单的完整代码

class Search : Fragment() {

var searchAdapter: SearchAdapter? = null

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {
    // Inflate the layout for this fragment
    val view = inflater.inflate(R.layout.fragment_search, container, false)

    val toolbar = view.findViewById<Toolbar>(R.id.toolbar_search)

    // placing toolbar in place of action bar
    if(activity is AppCompatActivity){
        (activity as AppCompatActivity).setSupportActionBar(toolbar)
    }


    val recyclerView = view.findViewById<RecyclerView>(R.id.search_result_tv)

    val model = ViewModelProviders.of(this).
            get(picodiploma.dicoding.database.picodiploma.dicoding.database.search.adapter.SearchView::class.java)

    model.getData().observe(this, Observer {
        resultsItem ->

        val layoutManager = LinearLayoutManager(context)
        recyclerView.layoutManager = layoutManager

        searchAdapter = SearchAdapter((resultsItem as …
Run Code Online (Sandbox Code Playgroud)

android android-fragments

-1
推荐指数
1
解决办法
2697
查看次数

Recyclerview 仅显示第一项

我的 recyclerview 仅显示 ArrayList 中的第一个项目,当我调试适配器时,它显示 getItemCount() 为 3,看起来不错,据我所知没有运行错误,请帮助:

\n\n

这是我的适配器

\n\n
class MyAdapter(private val myDataset: ArrayList<String>) :\n    RecyclerView.Adapter<MyAdapter.Holder>() {\n\n\n    override fun onCreateViewHolder(parent: ViewGroup,\n                                    viewType: Int): Holder {\n\n        val view = LayoutInflater.from(parent.context)\n            .inflate(R.layout.list_item, parent, false)\n\n        return Holder(view)\n    }\n\n\n    override fun onBindViewHolder(holder: Holder, position: Int) {\n        holder.textView?.text = myDataset[position]\n    }\n\n\n    override fun getItemCount() = myDataset.size\n\n\n    class Holder(itemView: View) : RecyclerView.ViewHolder(itemView) {\n        var textView:TextView? = null\n\n        init {\n            textView = itemView.findViewById(R.id.name)\n        }\n\n    }\n\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是片段:

\n\n
class FragmentList1 : Fragment() {\n\n    var myArrayList: …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-fragments kotlin android-recyclerview

-1
推荐指数
1
解决办法
1762
查看次数

我应该如何在两个片段中使用 ViewModel?

我有一个包含一个活动和两个片段的应用程序,在第一个片段中,我应该能够将数据插入数据库,在第二个片段中,我应该能够在 recyclerView 中看到添加的项目。

所以我制作了数据库、我的 RecyclerView 适配器和 ViewModel,

现在的问题是我应该如何管理这一切?

我应该在活动中初始化 ViewModel 并从片段中以某种方式调用它以使用插入吗?

我应该在两个片段中初始化视图模型两次吗?

我的代码如下所示:

让我们假设我在我的 Activity 中初始化了 viewholder:

class MainActivity : AppCompatActivity() {
     private val articoliViewModel: ArticoliViewModel by viewModels {
        ArticoliViewModelFactory((application as ArticoliApplication).repository)
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我的 FirstFragments 方法,我应该使用 viewModel 将数据添加到数据库中,如下所示:

class FirstFragment : Fragment() {
    private val articoliViewModel: ArticoliViewModel by activityViewModels()
    private fun addArticolo(barcode: String, qta: Int) { // function which add should add items on click
      // here i should be able to do something like this

        articoliViewModel.insert(Articolo(barcode, qta))
    } …
Run Code Online (Sandbox Code Playgroud)

android android-fragments kotlin android-viewmodel

-1
推荐指数
1
解决办法
188
查看次数