我想从MainActivity打开一个片段,我有gridview.i是android的新手,这是第一次使用片段。 这是我的主要活动:
package com.example.grasu.petrosani;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ThemedSpinnerAdapter;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener,AdapterView.OnItemClickListener{
private DrawerLayout DrawerLayout;
private NavigationView Drawer;
GridView gridview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Drawer = findViewById(R.id.nav_view);
Drawer.setNavigationItemSelectedListener(this);
DrawerLayout = findViewById(R.id.drawer_layout);
CategoryAdapter adapter = new CategoryAdapter(this, getSupportFragmentManager());
gridview=findViewById(R.id.album_Gridview);
gridview.setAdapter(new GridViewAdapter(this));
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
return false;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (parent.getId()==R.id.album_Gridview){
switch (position) {
case 0:
}
}
}
}
this is my xml for activity main:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<GridView
android:id="@+id/album_Gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="2"
android:layout_marginTop="10dp">
</GridView>
</FrameLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:duplicateParentState="true"
app:menu="@menu/drawer_view"/>
</android.support.v4.widget.DrawerLayout>
这是我想从gridview打开的片段:
package com.example.grasu.petrosani;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class InfoFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.info, container, false);
return rootView;
}}
我尝试使用FragmentManager fm = getSupportFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); ft.replace(CurrentClass.this,MainActivity.class);但是它没有用。我必须修改我的mainactivity来扩展fragmentActivity吗?
答案 0 :(得分:0)
你可以试试像 -
创建新活动。
在上面创建的活动中添加片段。
在您的GridView项目点击中,通过意图打开新活动,例如 - startActivity(new Intent(MainActivity.this, NewActivity.class));
答案 1 :(得分:0)
请按照以下代码:
@a={};File.foreach('f1.txt') do |f| # foreach ensures reading line by line without slurping the entire file into memory
# add first word as key = create array with rest of data and add note if it is number or stirng
@a[f.split.first] = [f.split.last, (f.scan(/\d+/).empty?) ? "string" : "number"]
puts
end
=> {"Number"=>["1", "number"], "Date"=>["01/01/2001", "number"], "Name"=>["John", "string"], "Age"=>["20", "number"], "Job"=>["Programmer", "string"]}