我在两个标签(在碎片上)制作自定义列表视图时遇到麻烦,我尝试使用类似的代码,我在Activity中使用自定义listView,但对于标签它不起作用。 TNX
Activity whit两个标签:
public class List_view_meni_mesni_meniji extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_meni__mesni__meniji__izbran);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewByid(R.id.fab1);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(List_view_meni_mesni_meniji.this, MojKroznik.class);
startActivity(intent);
}
});
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs_mesni_meni);
tabLayout.addTab(tabLayout.newTab().setText("PONUDBE"));
tabLayout.addTab(tabLayout.newTab().setText("PRILJUBLJENI"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager_mesni_meni);
final PagerAdapterMesniMeni adapter = new PagerAdapterMesniMeni(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
System.out.println("pritisnili smo back");
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
//@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
System.out.println("tukaj sem");
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.meniji) {
System.out.println("skocim na activity");
Intent intent = new Intent(this,MeniMainActivity.class);
startActivity(intent);
} else if (id == R.id.jedi) {
Intent intent = new Intent(this,JediMainActivity.class);
startActivity(intent);
} else if (id == R.id.pijaca) {
Intent intent = new Intent(this,PijaceMainActivity.class);
startActivity(intent);
} else if (id == R.id.akcije) {
Intent intent = new Intent(this,AkcijeMainActivity.class);
startActivity(intent);
} else if (id == R.id.moj_krožnik){
Intent intent = new Intent(this,MojKroznik.class);
startActivity(intent);
}
else if (id == R.id.pomoc) {
} else if (id == R.id.oNas) {
} else if(id == R.id.nastavitve) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
片段1(tab1)布局xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.approw.fastorder1.Fragment_Mesni_Meni_1">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list_meni1"/>
</RelativeLayout>
Fragment 1 java code:
package com.example.approw.fastorder1;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class Fragment_Mesni_Meni_1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,BundlesavedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment__mesni__meni_1, container, false);
}
}
自定义行布局xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_margin="20dp"
android:text=""
android:textColor="@android:color/black"
android:id="@+id/naslov_mesni_meni_1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:text=""
android:textColor="@android:color/black"
android:id="@+id/cena_mesni_meni_1"
android:layout_gravity="right"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>